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:ƒ ()
complete:ƒ ()
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".
ContentResultwith the custom error message written to the response body. could you show more of what you're doing in the controller part?