0

I am trying to send some response from Php script to Ajax, but it is not getting sent.Below is my code.

For now I am sending dummy data which I will change later..

Php Script..

        $response = ['Successfull callback','ndnvnvnsobdboi','ijjijbujbhu'];
       
        echo json_encode($response);
        
        die();

Ajax Script...

jQuery(document).ready(function($){
$("#login_otp_btn").click(function(event){
    val();
  event.preventDefault();
  jQuery.ajax({
            url: wpac_ajax_url.ajax_url,
            type:'POST',
            data :{
                dataType:'JSON',
                action : 'login_ajax_callback',
                login : $("#login_mobile").val(),
                
               success:function(response){
                    console.log("url wpac " + wpac_ajax_url.ajax_url); 
                    if(!response == ""){
                    response = JSON.parse( response );
                    console.log(response);
                    }else{
                        console.log("Response is empty");
                         }
                    },
                    });
                    $("#login_otp_field").show();   
                    })
                }); 

I am getting Response is empty in my Console window. Which is in else statement

3
  • What's wpac_ajax_url.ajax_url? Is it the admin_ajax.php endpoint? Are you registering the action handler with both wp_ajax_ and wp_ajax_nopriv_ ? Can you verify if your handler is actually being called, e.g. by tracing out with an error_log? And can you check the network tab in your debug tools to make sure the request is completing with 200 OK?
    – Rup
    Dec 22, 2020 at 11:34
  • 1
    Your problem is in this line: if(!response == ""){ the ! sign is not at the correct place, i.e: if(response != ""){
    – tdj
    Dec 22, 2020 at 11:34
  • Issue Solved.....I was missing the closing brace for data
    – Shehryar
    Dec 22, 2020 at 12:51

1 Answer 1

-1

Issue Solved I was missing a closing brace for data...

    jQuery(document).ready(function($){
    $("#login_otp_btn").click(function(event){
    val();
    event.preventDefault();
 jQuery.ajax({
        url: wpac_ajax_url.ajax_url,
        type:'POST',
        data :{
            dataType:'JSON',
            action : 'login_ajax_callback',
            login : $("#login_mobile").val(),
            }

           success:function(response){
                console.log("url wpac " + wpac_ajax_url.ajax_url); 
                if(response !== ""){
                response = JSON.parse( response );
                console.log(response);
                }else{
                    console.log("Response is empty");
                     }
                },
                });
                $("#login_otp_field").show();   
                })
            }); 
1
  • if(!response == ""){ is still incorrect. Dec 23, 2020 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.