简体   繁体   English

在 ASP.NET MVC 5 中调用 ajax 后返回单个错误消息

[英]Return individual error message after an ajax call in ASP.NET MVC 5

When I send an ajax request to the server, I want to return an individual message to the client in the responseText in case of an error.当我向服务器发送 ajax 请求时,我想在 responseText 中向客户端返回一条单独的消息,以防出错。 In debug mode on my development machine this works fine.在我的开发机器上的调试模式下,这工作正常。 Unfortunately, in production mode on the web server I always get an error message "Bad Request" but no longer the individual message.不幸的是,在 web 服务器的生产模式下,我总是收到错误消息“错误请求”,但不再是单个消息。 I am developing my application in ASP.NET MVC 5 and I am using jQuery 3.6.0.我正在 ASP.NET MVC 5 中开发我的应用程序,我正在使用 jQuery 3.6.0。

My ajax request looks like this:我的 ajax 请求如下所示:

$.ajax({
    type: 'POST',
    url: 'myURL',
    data: {
        val1: clientVal1,
        val2: clientVal2
    },
    success: function (res) {
        //do smthg...
    },
    error: function (response) {
        alert(response.responseText);
    }
});

On the server side, I take the ajax call like this:在服务器端,我使用 ajax 调用如下:

[HttpPost]
public ActionResult myURL(string val1, string val2)
{
    if(val1.contains(val2))
    {
                        
    }
    else
    {
        Response.StatusCode = 400;
        Response.Write("My custom error msg.");
        return new HttpStatusCodeResult(400);
    }
    return Json(new { someVal1, otherVal2}, JsonRequestBehavior.AllowGet);
}

The httperrors in my webconfig file look like this:我的 webconfig 文件中的 httperrors 如下所示:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <remove statusCode="403" subStatusCode="-1" />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="404" path="/ErrorHandling/http404" responseMode="ExecuteURL" />
    <error statusCode="403" path="/ErrorHandling/http403" responseMode="ExecuteURL" />
    <error statusCode="500" path="/ErrorHandling/http500" responseMode="ExecuteURL" />
</httpErrors>

What am I doing wrong?我究竟做错了什么?

I have found the solution.我找到了解决方案。 First I had to move the redirection to the individual error pages in the web.config to the <system.web> area.首先,我必须将重定向到 web.config 中的各个错误页面移动到 <system.web> 区域。 Now my system.web area looks like this ([...] means that there are other settings which are not relevant for this):现在我的 system.web 区域看起来像这样([...] 表示还有其他与此无关的设置):

<system.web>
    [...]
    <customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="/ErrorHandling/http500">
        <error statusCode="404" redirect="/ErrorHandling/http404" />
        <error statusCode="403" redirect="/ErrorHandling/http403" />
        <error statusCode="500" redirect="/ErrorHandling/http500" />
    </customErrors>
    [...]   
</system.web>

After that, I had to change the system.webServer section as suggested in the post by freedomn-m and suggested by Rahul Sharam as follows:之后,我不得不按照 freen-m 的帖子中的建议和 Rahul Sharam 的建议更改 system.webServer 部分,如下所示:

<httpErrors errorMode="Custom" existingResponse="PassThrough">
</httpErrors>

Now everything works as it should.现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM