I'm having a problem sending a custom message on error to an ajax call. My controller returns something like this:
return new HttpStatusCodeResult(400, "My error!");
And my ajax code looks like this:
error: function (xhr, httpStatusMessage) {
console.log(xhr.statusText);
console.log(httpStatusMessage);
}
The problem is that xhr.statusCode and httpStatusMessage is always "error". What am I doing wrong now? I'm expecting to have "My error!" in xhr.statusText.
I'm using ASP.NET MVC 5
and jquery-1.10.2
My xhr output is:
abort:ƒ ( statusText )
always:ƒ ()
plete:ƒ ()
done:ƒ ()
error:ƒ ()
fail:ƒ ()
getAllResponseHeaders:ƒ ()
getResponseHeader:ƒ ( key )
overrideMimeType:ƒ ( type )
pipe:ƒ ( /* fnDone, fnFail, fnProgress */ )
progress:ƒ ()
promise:ƒ ( obj )
readyState:4
responseText:"Bad Request"
setRequestHeader:ƒ ( name, value )
state:ƒ ()
status:400
statusCode:ƒ ( map )
statusText:"error"
success:ƒ ()
then:ƒ ( /* fnDone, fnFail, fnProgress */ )
My Web.config httpErrors configuration looks like this:
<httpErrors existingResponse="PassThrough" errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
<remove statusCode="403" />
<error statusCode="403" path="/Error/Forbidden" responseMode="ExecuteURL" />
</httpErrors>
and still, on my dev environment, the responseText is empty and the statusText is just "error".
I'm having a problem sending a custom message on error to an ajax call. My controller returns something like this:
return new HttpStatusCodeResult(400, "My error!");
And my ajax code looks like this:
error: function (xhr, httpStatusMessage) {
console.log(xhr.statusText);
console.log(httpStatusMessage);
}
The problem is that xhr.statusCode and httpStatusMessage is always "error". What am I doing wrong now? I'm expecting to have "My error!" in xhr.statusText.
I'm using ASP.NET MVC 5
and jquery-1.10.2
My xhr output is:
abort:ƒ ( statusText )
always:ƒ ()
plete:ƒ ()
done:ƒ ()
error:ƒ ()
fail:ƒ ()
getAllResponseHeaders:ƒ ()
getResponseHeader:ƒ ( key )
overrideMimeType:ƒ ( type )
pipe:ƒ ( /* fnDone, fnFail, fnProgress */ )
progress:ƒ ()
promise:ƒ ( obj )
readyState:4
responseText:"Bad Request"
setRequestHeader:ƒ ( name, value )
state:ƒ ()
status:400
statusCode:ƒ ( map )
statusText:"error"
success:ƒ ()
then:ƒ ( /* fnDone, fnFail, fnProgress */ )
My Web.config httpErrors configuration looks like this:
<httpErrors existingResponse="PassThrough" errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
<remove statusCode="403" />
<error statusCode="403" path="/Error/Forbidden" responseMode="ExecuteURL" />
</httpErrors>
and still, on my dev environment, the responseText is empty and the statusText is just "error".
Share Improve this question edited Jan 4, 2018 at 13:36 Robert asked Jan 4, 2018 at 13:09 RobertRobert 3132 gold badges16 silver badges37 bronze badges 7- Check the console for the error. Also check the response text of the request there too as it should give you the exception – Rory McCrossan Commented Jan 4, 2018 at 13:13
- ResponseText is "Bad Request" as you can see. – Robert Commented Jan 4, 2018 at 13:18
-
this answer is suggesting that you should send an empty
ContentResult
with the custom error message written to the response body. could you show more of what you're doing in the controller part? – Cee McSharpface Commented Jan 4, 2018 at 13:23 - I tried all of that and it still doesn't work. The thing is that I uploaded on azure and there I get the custom message just right so the problem is on my local dev environment. – Robert Commented Jan 4, 2018 at 13:25
- look into this and this. I guess its one of those settings messing with the custom errors behavior of IIS. – Cee McSharpface Commented Jan 4, 2018 at 13:30
1 Answer
Reset to default 7You need to set a property in your Web.Config file.
Citing a user of this web page on github, emphasis mine,
By default IIS will mask your error codes and replace them with default errors. The "PassThrough" option tells IIS to leave your custom errors alone and render them as-is.
"Bad Request" is the default http error text for the status code 400.
So there is the setting as documented here and outlined here,
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
</system.webServer>
</configuration>
Consult the documentation carefully for your version of IIS, there is lots of subtle version differences.
EDIT
Not really specific to MVC, but it is how I once solved it (part of production code), and what seems to have helped OP as well:
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.ContentType = "text/plain";
Response.Write(new String('_', 513) + "my custom message");
This absurd minimum character limit thing may or may not be needed depending on IIS version. I would too be grateful if somebody could shed a little more light on this underdocumented behavior.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351247a4623845.html
评论列表(0条)