简体   繁体   中英

Handling raw JavaScript being returned to .ajax error function despite 200 OK

I'm making the following jQuery.ajax() call to a product I have no control over:

$.ajax({
    type: "POST",
    url: "/WfWsR",
    data: { method: 'getInfo',
        nodeID: nodeID
    },
    dataType: "text",
    success: function(data, response, replyxhr){
        return data;
    },
    error: function(replyxhr, response){
        console.log(response);
        console.log(replyxhr);
        return response;
    }
});

The POST succeeds with a 200 OK, but instead of going to the success function it ends up in error, with the response variable set to "error" and the replyxhr variable set to Object { readyState=0, status=0, statusText="error"}.

Trying the POST manually through Postman yields the result:

new Array(
  new Array(
    new Array(
      "15", "1", ""
    )
  ), 
  new Array(
    new Array(
      "1", "3757", "3757", "user", "2013-01-22 15:09:04.354"
    )
  ), 
  new Array(
  ), 
  new Array(
    "3762", "ABCD", "test Purge Documents", "50", "purge Documents", "Administrator", "2013-01-22 15:07:57.065"
  ), "13d886ddf90"
)

That's a lot of JavaScript; technically I only need one item from any of those arrays. The reason I believe it's not working in jQuery is because that sort of return is probably invalid, especially given the dataType: "text". But I can't use dataType: "script" either; that uses an implicit GET instead of a POST, and the URL in question does not support GET.

Are there any other options I'm missing?

I am going to take a guess that you're telling Ajax to expect text but it is getting something else, according to the content-type of the response. It appears the server is giving you Javascript so I wouldn't be surprised if the content-type is text/javascript .

I've never had to deal with this but if this is your problem, the docs do say that there is a solution in jQuery 1.5 and higher:

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

via http://api.jquery.com/jQuery.ajax/ (see the doc for dataType)

That means you can possibly use this: dataType: "script text" to have a Javascript response interpreted as text. Of course, you could also just use dataType: "script" to have it interpreted as script.

Ended up writing my own xhr call (sans error-handling) to allow this. Terrible solution to a terrible problem. (Double fail for the eval()).

var xhReq = new XMLHttpRequest();
var params = "method=getInfo&nodeID=" + nodeID;

xhReq.open("POST", "/WfWsR", true);
xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhReq.onreadystatechange = onPostSubmit;
xhReq.send(params);
var response = eval(xhReq.response);
var stepIDx = response[0][0][0];

return stepIDx;

function onPostSubmit() {
    if (xhReq.readyState==4 || xhReq.readyState=="complete") { 
        if (xhReq.status != 200) {
            alert('BadStatus');
            return;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM