Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

I’m using my DataService and AjaxService for JavaScript and upgraded to jQuery 1.9.1. One of my post methods, which was working before, started hitting the deferred fail, instead of the deferred’s done, even though the status code was 200. This method is not returning any JSON to the caller. The 1.9 upgrade guide states “Prior to 1.9, an ajax call that expected a return data type of JSON or JSONP would consider a return value of an empty string to be a success case, but return a null to the success handler or promise. As of 1.9, an empty string returned for JSON data is considered to be malformed JSON (because it is); this will now throw an error. Use the error handler to catch such cases.”. This question on the forums lead me to the upgrade guide and “Identifier” says to return a 204 instead. I change my WebApi method to return a 204 and now it’s working again.

I now am using return this.Request.CreateResponse(HttpStatusCode.NoContent);

Here’s the method:

[System.Web.Mvc.HttpPost]
public HttpResponseMessage UpdateAlias(JObject jsonData)
{
    // http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling
    // http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods
    int assetId = 0;
    int userId = 0;
    string alias = string.Empty;
    try
    {
        assetId = jsonData["AssetId"].Value<int>();
        userId = jsonData["UserId"].Value<int>();
        alias = jsonData["Alias"].Value<string>();
        if (assetId == 0)
        {
            throw new ArgumentException("Asset Id is required.");
        }

        if (userId == 0)
        {
            throw new ArgumentException("User Id is required.");
        }

        if (string.IsNullOrWhiteSpace(alias))
        {
            throw new ArgumentException("Alias is required and cannot be empty.");
        }
        
        // more code here…
        // notice the noContent
        return this.Request.CreateResponse(HttpStatusCode.NoContent);
    }
    catch (Exception ex)
    {
        this._logger.Warn(string.Format("Error in UpdateAlias assetId:{0}, userId:{1}, new alias: {2}", assetId, userId, alias), ex);
        throw new HttpResponseException(new HttpResponseMessage
                                        {
                                            StatusCode = HttpStatusCode.InternalServerError,
                                            Content = new StringContent("Error!")
                                        });
    }
}

Note: I was not able to use the NoContent with a post in an MVC Controller. It was returning 200, so jQuery was always hitting the fail and I don't know why.So I just moved that method into my WebAPI. Once I did that I got the expected 204.
Posted on Wednesday, May 8, 2013 9:41 AM JavaScript , jQuery | Back to top


Comments on this post: jQuery 1.9.1+ Ajax Post is Failing even though it has a 200 Status

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net