標籤:str options type style 項目 blog erro script alert
項目中common.js中有一段ajax異常處理方法:
$(document).ajaxComplete(function(event,xhr,options) { if (xhr.statusText=="parsererror") { top.location.href="/admin"; } });
有一個異常頁面的ajax請求如下:
$.ajax({ type :"post", url :"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", timeout : 40000, data: "null", error : function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); alert(‘讀取逾時,請檢查網路連接‘); }, dataType : "json", success : function(data){ alert("1"); } });
上述的代碼一直進入error,並且
alert(textStatus);
彈出的一直是parsererror
alert(XMLHttpRequest.status);
彈出200正常
alert(XMLHttpRequest.readyState);
彈出4正常
最後發現彈出parsererror的原因是類型的問題,也就是說可能是傳回型別出問題了。
果然進過尋找,發現在struts.xml檔案中的ajax請求的action中的result傳回型別不是json.而是跳轉頁面String類型。
記錄此次異常處理,以上。
JQuery ajax請求一直返回Error(parsererror)