![](/img/trans.png)
[英]jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK
[英]ajax returns an error even if everything is ok
我有這個簡單的代碼
數據定期保存在服務器上,但始終生成錯誤“錯誤ListaSegnalazioni!未定義”
如果我刪除指令e.preventDefault();
錯誤消失了!
可能是什么問題呢?
e.preventDefault();
$.ajax({
type: "POST",
url: '/api/PostListaSegnalazioni/',
data: JSON.stringify(rows),
"dataSrc": "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert("Dati Inviati!");
},
error: function(xhr, status, error) {
alert("Error ListaSegnalazioni! " + xhr.responseText);
}
});
***難以置信!!*****
我修改了代碼,錯誤消失了!
$.ajax({
type: "POST",
url: '/api/PostListaSegnalazioni/',
data: JSON.stringify(rows),
"dataSrc": "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("Dati Inviati!");
},
error: function (xhr, status, error) {
alert(xhr.responseText.includes("undefined"));
if (xhr.responseText != "undefined") { alert("Error ListaSegnalazioni! " + xhr.responseText);}
}
});
插入指令alert(xhr.responseText.includes("undefined"));
就足夠了alert(xhr.responseText.includes("undefined"));
非常非常奇怪的行為!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.