This is my JS code.
This is my C# code. (It is intentionally setting an error code)
I always get a success response.
edit: This has been solved. You need to actually set the HttpResponse.Status
so that it is catched in the error block
I believe returning using Json() overrides the return status as Success.
Try this.
https://stackoverflow.com/questions/11370251/return-json-with-error-status-code-mvc/43069917
The first thing that stands out is that your C# method is AddPassword and your ajax post request url is /Pages/VerifyPassword.
There’s nothing in the text you chopped up for the C# code which would indicate if you have an attribute route in use and there’s no definition for the message variable so I can’t speak to what might be in there.
Are you sure the controller method you’re expecting is actually being called by the ajax request?
Given the usage of System.Web.Http instead of System.Web.Mvc, your return might be IHttpActionResult, but either way, setting the Response StatusCode should return a value so I can only assume that the controller isn’t being called the way you want it to be.
Not sure how others are handling it but, in my controller I usually ‘throw new HttpResponseException(HttpStatusCode.Whatever)’ or as part of the throw, create a custom response message. For dupe’s, I throw a conflict. For missing dependant data, I throw a NotFound. For missing critical fields, I throw a NotAcceptable. It’s my own scheme. I believe the throw immediately exits the function. If you need a custom message for more detail, use the custom response message with a more detailed reasonPhrase populated with your own string. Then in your js, set a breakpoint on the error function(response) or within that function and take a look at the content of the response. You’ll see properties like statusCode and reasonPhrase (I think) which you can then switch case off of and handle as you see fit. GLHF!
C# devs
null reference exceptions