欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息

程序员文章站 2024-02-23 13:58:40
一、如何用ajax调用jsonresult方法 比如fuckcontroller中添加有个返回jsonresult类型的方法fuckjson(): 复制代码 代码如下:...

一、如何用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>

如果我们直接在浏览器里调用,是可以看到结果的:

ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息

因为在浏览器地址栏直接输入这个地址,是一次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>

ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息

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格式的错误信息。

ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息

ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息