[問題] 請問Exception相關問題(ashx 搭配jQuery)

看板Ajax作者 (valda)時間13年前 (2012/08/24 17:58), 編輯推噓2(207)
留言9則, 5人參與, 最新討論串1/2 (看更多)
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

08/24 18:09, , 1F
4xx 5xx ???
08/24 18:09, 1F

08/24 18:50, , 2F

08/24 21:43, , 3F
錯誤被抑制,回傳的http code 還是200,當然跑success啊…
08/24 21:43, 3F

08/24 23:04, , 4F
所以建議ashx端不要加try catch 嗎?
08/24 23:04, 4F

08/25 10:09, , 5F
我不懂ashx不過你在success中加個條件判定讓它導向你
08/25 10:09, 5F

08/25 10:10, , 6F
要的東西(只要你有辦法判定格式不正確)然後return,
08/25 10:10, 6F

08/25 10:11, , 7F
這樣可行嗎?
08/25 10:11, 7F

08/25 12:31, , 8F
如果你要用http code來判斷的話,在 catch 那裡加上
08/25 12:31, 8F

08/25 12:33, , 9F
throw new HttpException(500, 訊息)
08/25 12:33, 9F
文章代碼(AID): #1GDr0wwG (Ajax)
文章代碼(AID): #1GDr0wwG (Ajax)