我能夠得到實際的錯誤消息之前,當我使用jQuery的ajax + asp.net的Web服務。但是,jquery $ ajax錯誤中的相同代碼不再有效。捕獲jQuery中的錯誤消息ajax - 從asp.net mvc行動
裏面我的.js我有
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Controller/DoSomething",
data: JSON.stringify({ varname: varvalue }),
cache: false,
dataType: "json",
success: function (wo) {
alert('yay!');
},
error: function (xhr) {
alert('error');
if (xhr.responseText) {
var err = JSON.parse(xhr.responseText);
if (err) {
alert(err.Message);
}
else {
alert("Unknown server error, please try again!");
}
}
}
});
在我的控制器我有
public JsonResult DoSomething(string folderno)
{
CustomObject obj;
//get the obj value from repository here
throw new Exception("my test error message");
return Json(obj);
}
我看了看螢火,看來,我越來越 「JSON.parse:意外的字符「錯誤。
我在這裏要做的是在從存儲庫獲取obj時引發異常情況。顯然,return Json(obj)
永遠不會達到。
我的問題是,我該如何處理這種情況並在JS端捕獲錯誤消息?我需要在我的控制器中做些什麼嗎?
在我之前設置的Jquery + asp.net Web服務中,我可以在我的Web服務方法中引發一個異常(如我現在的操作中所示),它會被困在我的ajax錯誤中,並且錯誤消息會被解析出來。
現在,看起來我需要捕捉異常並收拾自己....問題是怎麼回事?我是否需要在每個動作中都這樣做?這看起來很多工作。
在這裏看到這個問題,什麼我落得這樣做 http://stackoverflow.com/questions/8249479/getting-properties-from-jsonresult-on-the-js-side-inside-jquery-ajax – sarsnake