今天,我面临一个从未遇到过的奇怪问题。我的ajax调用返回的响应状态为200,但是无论如何它总是触发错误事件而不是成功。这就是我正在做的
$.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
}
});
swap.php的PHP代码
<?php
header('Content-Type: application/json');
$resp["data"]="Record updated successfully";
print json_encode($resp);
?>
这是将网址直接放在地址栏中时得到的响应
下面是chrome的“网络”标签的屏幕截图
答案 0 :(得分:1)
您要显示的响应图像不是实际的请求,它是飞行前的检查,请按照您尝试的方法,检查Request方法是OPTIONS
而不是"GET"
。它只是检查此请求中的"CORS"
。不是实际的ajax调用。
在此之后检查实际请求。您会发现错误。
答案 1 :(得分:0)
您可以尝试传递自定义标头
headers: {
'Content-Type': 'application/json',
"Accept": 'application/json',
}