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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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();
})
});
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0