0

I have the following call:

var params={type:"PUT", dataType:"application/json; charset=UTF-8", url:"api/servletpat", data:JSON.stringify(dataObject)};
$.ajax(params)
    .done(function(data, status, jqXHR){
        successCallback(data);
    })
    .fail(function(jqXHR, status, thrown){
        if (jqXHR.status == 200){
            successCallback(null);
        }
    });

Although the server does send a JSON response, ajax executed the .fail case with jqXHR.status = 200. Which means the returned data is not accessible. I cannot use "GET" because GET encodes the submitted object in the URL and this is not acceptable. What do I need to do to be able to read the returned JSON object ? Thanks...

5
  • 3
    I would have thought that POST would be the next logical after GET Commented Jan 27, 2014 at 4:28
  • any error in your browser console Commented Jan 27, 2014 at 4:28
  • should be a server side error Commented Jan 27, 2014 at 4:29
  • user2310289, I am following guidelines for the API where POST is reserved for adding new objects, PUT for updating existing objects and GET to retrieve objects and metadata. Commented Jan 29, 2014 at 3:13
  • Arun P Johny, Emilio Gort, No error server or client side. Server executed the PUT, and returned proper return object and a success HTTP status (200). The only problem is that the JQuery' ajax call, reported this as a fail (still with success status). Commented Jan 29, 2014 at 3:16

3 Answers 3

2

You are specifying an invalid value for the dataType setting. This causes the .fail callback to get called (instead of the .done callback), even though the jqXHR.status is 200. The second parameter to the .fail callback function (the "textStatus" parameter) will be "parsererror".

jsfiddle demo that fails

You should have:

contentType: "application/json; charset=UTF-8",
dataType: "json",

jsfiddle demo that succeeds

With the contentType setting, you are specifying the content-type for the ajax request. When you set dataType to "json", you are telling jQuery that the response will be JSON. That causes jQuery to automatically parse the response into an object (or array or null) before passing it as the data parameter to the .done callback function.

Sign up to request clarification or add additional context in comments.

Comments

0

status 200 means OK, not that the data is not accessible http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

Comments

0

TYPE has to be either GET or POST

2 Comments

Is this a quote from somewhere?
@john Nope.not a quote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.