Response.StatusDescription not sent from JsonResult to jQuery

Please keep in mind that I'm pretty new to how MVC, Json, jQuery, etc. work, so bear with me. I have been doing web forms for the past 5 years ...

I am working on validating the form inside a Modal popup that uses the JsonResult method to submit form data to the server. I wish I could just load Partial View into this popup and do away with it, but this is not an option.

Anyway, I have some code that worked yesterday, but after I did pull / push with Git, something went wrong with my validation. I do some basic checks using normal JavaScript before passing anything to the server (required fields, correct data types, etc.), but some things, such as making sure that the name the user enters, is unique, require me to go all the way to business logic.

After searching the Internet, I found that if you want jQuery to recognize an error from JsonResult in an AJAX request, you must send an HTTP status code that is erroneous. I am pretty sure that it can be any number in 400 or 500, and it should work ... and it ... to the point.

What would I do, set the status code and status description using Response.StatusCode and Response.StatusDescription , and then return the model. JQuery recognizes the error, then displays the error message specified in the status description. Everything worked perfectly.

Today it seems that the only thing that makes it from my JsonResult in my controller for my jQuery is the status code. I traced through C # and everything seems to be configured correctly, but I just can't extract this custom description of the state that I set.

Here is the code I have:

Modal popup

 <fieldset id="SmtpServer_QueueCreate_Div"> @Form.HiddenID("SMTPServerId") <div class="editor-label"> @Html.LabelFor(model => model.ServerName) <br /> <input type="text" class="textfield wide-box" id="ServerName" name="ServerName" title="The display name of the Server" /> <br /> <span id="ServerNameValidation" style="color:Red;" /> </div> <div class="editor-label"> <span id="GeneralSMTPServerValidation" style="color:Red;" /> </div> <br /> <p> <button type="submit" class="button2" onclick="onEmail_SmtpServerQueueCreateSubmit();"> Create SMTP Server</button> <input id="btnCancelEmail_SmtpServerQueueCreate" type="button" value="Cancel" class="button" onclick="Email_SmtpServerQueueCreateClose();" /> </p> </fieldset> 

Controller

  [HttpPost] public virtual JsonResult _QueueCreate(string serverName) { Email_SmtpServerModel model = new Email_SmtpServerModel(); string errorMessage = ""; try { Email_SmtpServer dbESS = new Email_SmtpServer(ConnectionString); model.SMTPServerId = System.Guid.NewGuid(); model.ServerName = serverName; if (!dbESS.UniqueInsert(model, out errorMessage)) { return Json(model); } } catch (Exception ex) { errorMessage = ex.Message; } Response.StatusCode = 500; Response.StatusDescription = errorMessage; return Json(model); } 

JQuery Ajax request

 $.ajax({ type: 'POST', data: { ServerName: serverName }, url: getBaseURL() + 'Email_SmtpServer/_QueueCreate/', success: function (data) { onSuccess(data); }, error: function (xhr, status, error) { $('#GeneralSMTPServerValidation').html(error); } }); 

As I mentioned, yesterday it showed a nice message to the user informing them that the name they entered was not unique if this happened. Now all I get is the message "Internal server error" ... this is correct, because this is what I send with me when I set my status code. However, as I mentioned, he no longer sees the user-defined description of the state that I am sending.

I also tried setting it to some unused status code to make sure that this is a problem, but it just doesn’t show anything because I don’t know what text will be displayed.

Who knows? Maybe something is wrong with my code. Most likely, it was a change made somewhere else that it could be, I have no idea. Does anyone have any ideas on what could go wrong?

If you need another code, I will try to provide it.

Thanks!

+6
source share
1 answer

In error callback try using

 xhr.statusText 

In addition, if you use the Visual Studio web server, you cannot return the status text. http://forums.asp.net/post/4180034.aspx

+10
source

Source: https://habr.com/ru/post/903922/


All Articles