请在这种奇怪的情况下帮助我.这是我第一次看到,if {} {}后,else {}无效.=)))
onClick="$.post('itemupdate.php', { itemname: 'test' }, function(data) { if(data.status == 'error'){alert('error')} else {alert('OK')} }, 'json');"
Run Code Online (Sandbox Code Playgroud)
警报'确定'永远不会出现.其他一切都井然有序.
如果使用jQuery.post()的请求返回错误代码,它将无声地失败,除非该脚本还调用了全局.ajaxError()方法或.从jQuery 1.5开始,jQuery.post()返回的jqXHR对象的.error()方法也可用于错误处理.
http://api.jquery.com/jQuery.post/
另外:如果要访问请求状态,回调函数将获得3个参数:data,textStatus,jqXHR.如果你返回糟糕的json,你的数据是null或未定义的,data.status将失败.
这是正确的代码.另外,我建议你不要把你的代码放在html属性中!
$.post('itemupdate.php',
{ itemname: 'test' },
function(data, status) {
if (status == 'error') {
alert('error');
}
else {
alert('OK');
}
},
'json'
);
Run Code Online (Sandbox Code Playgroud)