Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.StatusDescription not being sent from JsonResult to jQuery

Please keep in mind that I'm rather new to how MVC, Json, jQuery, etc. works, so bear with me. I've been doing Web Forms for the past 5 years...

I'm working on validating a form within a Modal popup that uses a JsonResult method for posting the form data to the server. I wish I could have just loaded a Partial View in that popup and be done with it, but that's not an option.

Anyway, I have some code that was working yesterday, but after I did a pull / push with Git, something went a bit wrong with my validation. I do some basic validation with regular JavaScript before I pass anything to the server (required fields, correct data types, etc.), but some things, like making sure the name the user types in is unique, require me to go all the way to the business logic.

After poking around the internet, I discovered that if you want jQuery to recognize an error from a JsonResult in an AJAX request, you must send along a HTTP Status Code that is of an erroneous nature. I'm fairly certain it can be any number in the 400's or 500's and it should work...and it does...to a point.

What I would do is set the Status Code and Status Description using Response.StatusCode and Response.StatusDescription, then return the model. The jQuery would recognize an error, then it would display the error message I set in the status description. It all worked great.

Today, it seems that the only thing that makes it from my JsonResult in my controller to my jQuery is the Status Code. I've traced through the c# and everything seems to be set correctly, but I just can't seem to extract that custom Status Description I set.

Here is the code I have:

The 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>

The 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);
        }

The 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);
            }
        });

Like I mentioned, yesterday, this was showing a nice message to the user informing them that the name they entered was not unique if it happened to exist. Now, all I'm getting is a "Internal Server Error" message...which is correct, as that's what I am sending along in my when I set my Status Code. However, like I mentioned, it no longer sees the custom Status Description I send along.

I've also tried setting it to some unused status code to see if that was the problem, but that simply shows nothing because it doesn't know what text to show.

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

If you need any more code, I'll try to provide it.

Thanks!

like image 738
user1059903 Avatar asked Dec 16 '11 16:12

user1059903


1 Answers

In your error callback, try using

xhr.statusText

Also, if you're using Visual Studio's webserver, you may not get the status text back. http://forums.asp.net/post/4180034.aspx

like image 195
Nick VanderPyle Avatar answered Nov 06 '22 17:11

Nick VanderPyle