請幫助我在這種奇怪的情況。 這是我第一次看到,其他{}在if(){}之後無效。 =)))
onClick="$.post('itemupdate.php', { itemname: 'test' }, function(data) { if(data.status == 'error'){alert('error')} else {alert('OK')} }, 'json');"
警報'OK'從不出現。其他一切都是爲了。
請幫助我在這種奇怪的情況。 這是我第一次看到,其他{}在if(){}之後無效。 =)))
onClick="$.post('itemupdate.php', { itemname: 'test' }, function(data) { if(data.status == 'error'){alert('error')} else {alert('OK')} }, 'json');"
警報'OK'從不出現。其他一切都是爲了。
剛做了一個叫ok的輸出!
if (mysql_error()) { $return_arr["status"] = 'error'; } else { $return_arr["status"] = 'ok'; } echo json_encode($return_arr);
現在事情奏效!
function(data) { if(data.status == 'error'){alert('error')} if(data.status == 'ok') {alert('OK')} }
如果有jQuery.post的請求()返回一個錯誤代碼,它會失敗 默默的,除非劇本也被稱爲全球.ajaxError() 方法或。從jQuery 1.5開始,jQuery.post()返回的jqXHR對象 的.error()方法也可用於錯誤處理。
http://api.jquery.com/jQuery.post/
另外:如果你想訪問請求狀態,回調函數獲取3個參數: 數據,textStatus,jqXHR。如果你返回蹩腳的JSON,你的數據爲空或未定義,並且data.status會失敗。
這是正確的代碼。另外,我建議你永遠不要把你的代碼放在html屬性中!
$.post('itemupdate.php',
{ itemname: 'test' },
function(data, status) {
if (status == 'error') {
alert('error');
}
else {
alert('OK');
}
},
'json'
);
換句話說,if/else語句沒有問題,問題在於成功回調僅在...上調用...好...成功... – nnnnnn
正確。閱讀手冊也是正確的做法;) –
實際上在我的情況下 - 當數據(腳本輸出)不爲空時調用回調。 –