看板 Ajax 關於我們 聯絡資訊
ashx code try { XXXXXXX context.Response.Write(JsonConvert.SerializeObject(dt)); } catch (Exception ex) { context.Response.Write(ex.ToString()); } jQuery $.ajax({ type: "post", url: "XXXX.ashx", data: 'StartMonth=' + $("#txtStartMonth").val() + "&EndMonth=" + $("#txtEndMonth").val(), success: function (response, status, xhr) { ParseCTMData(response); }, error: function (xhr, status, error) { console.log('xhr---->'); console.log(xhr.status); console.log(xhr); console.log('<----xhr'); console.log('status---->'); console.log(status); console.log('<----status'); console.log('error---->'); console.log(error); console.log('<----error'); alert('error code:' + xhr.status + ',error message:' + error); } }) 如果ashx中真的有error產生,jQuery還是會掉到success中的迴圈,不會跑到error中 ,請問要如何設計exception handle較好呢? 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.130.45.113
ChowMein:4xx 5xx ??? 08/24 18:09
Ageis:錯誤被抑制,回傳的http code 還是200,當然跑success啊… 08/24 21:43
valda:所以建議ashx端不要加try catch 嗎? 08/24 23:04
hermitwhite:我不懂ashx不過你在success中加個條件判定讓它導向你 08/25 10:09
hermitwhite:要的東西(只要你有辦法判定格式不正確)然後return, 08/25 10:10
hermitwhite:這樣可行嗎? 08/25 10:11
Ageis:如果你要用http code來判斷的話,在 catch 那裡加上 08/25 12:31
Ageis:throw new HttpException(500, 訊息) 08/25 12:33