Today I am facing a strange issue that I never faced before. My ajax call is returning response with 200 status but somehow it always trigger error event instead of success. This is what I am doing
$.ajax({
type: "GET",
url: "swap.php",
data: "key=dbdcd39f0f8f5077e7308f3d3d5d8cac&code="+$("#code").val(),
contentType: 'application/json; charset=utf-8',
dataType:"json",
beforeSend: function() {
$("#msg").html("<img src='imgs/loading_circle.gif'/>");
},
success: function(result){
alert(result);
},
error: function(xhr,status,error){
alert(error.Message); // receiving Undefined message
}
});
PHP code of swap.php
<?php
header('Content-Type: application/json');
$resp["data"]="Record updated successfully";
print json_encode($resp);
?>
This is the response which I get when directly put the url in address bar
Below is the screenshot of chrome’s network tab
2
Answers
The response image you’re showing not the actual request, it’s a pre flight check, Check the Request method it’s
OPTIONS
not the"GET"
as what you are trying. it’s just checking for the"CORS"
in this request. Not the actual ajax call.Check the actual request, just after this. You will find the error.
you can try passing custom headers