程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET中MVC使用AJAX調用JsonResult方法並返回自定義錯誤信息

ASP.NET中MVC使用AJAX調用JsonResult方法並返回自定義錯誤信息

編輯:ASP.NET基礎

一、如何用AJAX調用JsonResult方法

比如FuckController中添加有個返回JsonResult類型的方法FuckJson():

復制代碼 代碼如下:
<span class="kwd">public<span class="pln"> <span class="typ">JsonResult<span class="pln"> <span class="typ">FuckJson<span class="pun">()<span class="pln">
<span class="pun">{<span class="pln">
    <span class="kwd">return<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">JsonResult<span class="pun">()<span class="pln">
    <span class="pun">{<span class="pln">
        <span class="typ">Data<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">List<span class="str"><string><span class="pun">()<span class="pln"> <span class="pun">{<span class="pln"> <span class="str">"fuck"<span class="pun">,<span class="pln"> <span class="str">"shit"<span class="pln"> <span class="pun">},<span class="pln"> <span class="typ">JsonRequestBehavior<span class="pln"> <span class="pun">=<span class="pln"> <span class="typ">JsonRequestBehavior<span class="pun">.<span class="typ">AllowGet<span class="pln">
    <span class="pun">};<span class="pln">
<span class="pun">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

如果我們直接在浏覽器裡調用,是可以看到結果的:

因為在浏覽器地址欄直接輸入這個地址,是一次GET請求,這就是為毛上面的代碼裡要寫JsonRequestBehavior.AllowGet。

用JQuery AJAX調用的代碼如下:

復制代碼 代碼如下:
<span class="pln">$<span class="pun">.<span class="pln">ajax<span class="pun">({<span class="pln">
    url<span class="pun">:<span class="pln"> <span class="str">"/Fuck/FuckJson"<span class="pun">,<span class="pln">
    data<span class="pun">:<span class="pln"> <span class="str">""<span class="pun">,<span class="pln">
    dataType<span class="pun">:<span class="pln"> <span class="str">"json"<span class="pun">,<span class="pln">
    type<span class="pun">:<span class="pln"> <span class="str">"POST"<span class="pun">,<span class="pln">
    contentType<span class="pun">:<span class="pln"> <span class="str">"application/json; charset=utf-8"<span class="pun">,<span class="pln">
    dataFilter<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pln"> <span class="pun">(<span class="pln">data<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
        <span class="kwd">return<span class="pln"> data<span class="pun">;<span class="pln">
    <span class="pun">},<span class="pln">
    success<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pln"> <span class="pun">(<span class="pln">data<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
        alert<span class="pun">(<span class="pln">data<span class="pun">);<span class="pln">
    <span class="pun">}<span class="pln">
<span class="pun">})</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

url: "/Fuck/FuckJson" 很顯然是Action的位置。data: "" 表示FuckJson()放方沒有參數。dataType: "json"是理所當然的,我們返回的就是JsonResult。

二、返回自定義錯誤消息

默認情況下,如果AJAX請求發生了錯誤。它最多只知道錯誤碼,而無法顯示具體的錯誤信息。但報錯通常是需要的,所以我們先要寫一個過濾器,作用是一旦發生exception,就給返回的json中的data屬性加一個errorMessage。並且,response的狀態不能是200,不然ajax請求會以為沒有發生錯誤。一般的服務器端錯誤代碼是500。這個過濾器函數如下:

復制代碼 代碼如下:
<span class="kwd">public<span class="pln"> <span class="kwd">class<span class="pln"> <span class="typ">JsonExceptionFilterAttribute<span class="pln"> <span class="pun">:<span class="pln"> <span class="typ">FilterAttribute<span class="pun">,<span class="pln"> <span class="typ">IExceptionFilter<span class="pln">
<span class="pun">{<span class="pln">
    <span class="kwd">public<span class="pln"> <span class="kwd">void<span class="pln"> <span class="typ">OnException<span class="pun">(<span class="typ">ExceptionContext<span class="pln"> filterContext<span class="pun">)<span class="pln">
    <span class="pun">{<span class="pln">
        <span class="kwd">if<span class="pln"> <span class="pun">(<span class="pln">filterContext<span class="pun">.<span class="typ">RequestContext<span class="pun">.<span class="typ">HttpContext<span class="pun">.<span class="typ">Request<span class="pun">.<span class="typ">IsAjaxRequest<span class="pun">())<span class="pln">
        <span class="pun">{<span class="pln">
            filterContext<span class="pun">.<span class="typ">HttpContext<span class="pun">.<span class="typ">Response<span class="pun">.<span class="typ">StatusCode<span class="pln"> <span class="pun">=<span class="pln"> <span class="lit">500<span class="pun">;<span class="pln">
            filterContext<span class="pun">.<span class="typ">ExceptionHandled<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">true<span class="pun">;<span class="pln">
            filterContext<span class="pun">.<span class="typ">Result<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">JsonResult<span class="pln">
            <span class="pun">{<span class="pln">
                <span class="typ">Data<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln">
                <span class="pun">{<span class="pln">
                    errorMessage <span class="pun">=<span class="pln"> filterContext<span class="pun">.<span class="typ">Exception<span class="pun">.<span class="typ">Message<span class="pln">
                <span class="pun">},<span class="pln">
                <span class="typ">JsonRequestBehavior<span class="pln"> <span class="pun">=<span class="pln"> <span class="typ">JsonRequestBehavior<span class="pun">.<span class="typ">AllowGet<span class="pln">
            <span class="pun">};<span class="pln">
        <span class="pun">}<span class="pln">
 
    <span class="pun">}<span class="pln">
<span class="pun">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

之後,我們要給FuckJson()方法加上這個“特征”(Attribute在C#中最好不要翻譯為屬性),然後故意拋個錯做測試。修改後的方法如下:

復制代碼 代碼如下:
<span class="pun">[<span class="typ">JsonExceptionFilterAttribute<span class="pun">]<span class="pln">
<span class="kwd">public<span class="pln"> <span class="typ">JsonResult<span class="pln"> <span class="typ">FuckJson<span class="pun">()<span class="pln">
<span class="pun">{<span class="pln">
    <span class="kwd">try<span class="pln">
    <span class="pun">{<span class="pln">
        <span class="kwd">throw<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">Exception<span class="pun">(<span class="str">"oh shit!"<span class="pun">);<span class="pln">
        <span class="kwd">return<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">JsonResult<span class="pun">()<span class="pln">
        <span class="pun">{<span class="pln">
            <span class="typ">Data<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">List<span class="str"><string><span class="pun">()<span class="pln"> <span class="pun">{<span class="pln"> <span class="str">"fuck"<span class="pun">,<span class="pln"> <span class="str">"shit"<span class="pln"> <span class="pun">},<span class="pln"> <span class="typ">JsonRequestBehavior<span class="pln"> <span class="pun">=<span class="pln"> <span class="typ">JsonRequestBehavior<span class="pun">.<span class="typ">AllowGet<span class="pln">
        <span class="pun">};<span class="pln">
    <span class="pun">}<span class="pln">
    <span class="kwd">catch<span class="pln"> <span class="pun">(<span class="typ">Exception<span class="pln"> ex<span class="pun">)<span class="pln">
    <span class="pun">{<span class="pln">
        <span class="kwd">throw<span class="pln"> ex<span class="pun">;<span class="pln">
    <span class="pun">}<span class="pln">
<span class="pun">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

捕獲異常後,你也可以先記一下log、或是根據你自己的需要處理一下這個異常,然後再throw。

最後我們的AJAX請求代碼也得做修改,增加一個error的處理:

復制代碼 代碼如下:
<span class="pln">$<span class="pun">.<span class="pln">ajax<span class="pun">({<span class="pln">
    url<span class="pun">:<span class="pln"> <span class="str">"/Fuck/FuckJson"<span class="pun">,<span class="pln">
    data<span class="pun">:<span class="pln"> <span class="str">""<span class="pun">,<span class="pln">
    dataType<span class="pun">:<span class="pln"> <span class="str">"json"<span class="pun">,<span class="pln">
    type<span class="pun">:<span class="pln"> <span class="str">"POST"<span class="pun">,<span class="pln">
    contentType<span class="pun">:<span class="pln"> <span class="str">"application/json; charset=utf-8"<span class="pun">,<span class="pln">
    dataFilter<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pln"> <span class="pun">(<span class="pln">data<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
        <span class="kwd">return<span class="pln"> data<span class="pun">;<span class="pln">
    <span class="pun">},<span class="pln">
    success<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pln"> <span class="pun">(<span class="pln">data<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
        alert<span class="pun">(<span class="pln">data<span class="pun">);<span class="pln">
    <span class="pun">},<span class="pln">
    error<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pln"> <span class="pun">(<span class="pln">fuckedObject<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
        <span class="kwd">try<span class="pln"> <span class="pun">{<span class="pln">
            <span class="kwd">var<span class="pln"> json <span class="pun">=<span class="pln"> $<span class="pun">.<span class="pln">parseJSON<span class="pun">(<span class="pln">fuckedObject<span class="pun">.<span class="pln">responseText<span class="pun">);<span class="pln">
            alert<span class="pun">(<span class="pln">json<span class="pun">.<span class="pln">errorMessage<span class="pun">);<span class="pln">
        <span class="pun">}<span class="pln"> <span class="kwd">catch<span class="pun">(<span class="pln">e<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln">
            alert<span class="pun">(<span class="str">'something bad happened'<span class="pun">);<span class="pln">
        <span class="pun">}<span class="pln">
    <span class="pun">}<span class="pln">
<span class="pun">})</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

現在請求這個Action,會發現Response的狀態已經是500了。並且返回了Json格式的錯誤信息。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved