【发布时间】:2014-12-19 06:14:13
【问题描述】:
即使下面的 PHP 代码因错误退出,AJAX 代码中的 success 回调仍然会被触发。这是为什么呢?
JavaScript 代码:
$.ajax({
type: "POST",
url: xxxx,
data: {info:data},
success: function(result){
//code here
},
error:function(msg)
{
alert('add data error,please add again');
}
});
php代码:
if(is_wrong) //data is error
{
exit("the data is not right,please add again");
}
【问题讨论】:
-
你的 PHP 代码是什么?
-
如果ajax请求成功,jquery ajax中的成功回调总是会被触发。这与您从 php 文件中获得或未获得的响应无关。如果 request 有错误,那么错误回调函数才会被执行。