简体   繁体   English

尽管200 OK,仍处理原始JavaScript返回到.ajax错误函数

[英]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: 我正在对无法控制的产品进行以下jQuery.ajax()调用:

$.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"}. POST以200 OK成功,但没有转到成功函数,而是以错误形式结束,将响应变量设置为“错误”,将replyxhr变量设置为Object {readyState = 0,status = 0,statusText =“错误”}。

Trying the POST manually through Postman yields the result: 通过邮递员手动尝试POST会产生以下结果:

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; 有很多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". 我认为它在jQuery中不起作用的原因是,这种返回可能无效,尤其是考虑到dataType:“ text”。 But I can't use dataType: "script" either; 但是我也不能使用dataType:“ script”; that uses an implicit GET instead of a POST, and the URL in question does not support GET. 使用隐式GET而不是POST的网址,所涉及的网址不支持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. 我要猜测的是,根据响应的content-type ,您正在告诉Ajax期待text但它还有其他作用。 It appears the server is giving you Javascript so I wouldn't be surprised if the content-type is text/javascript . 看来服务器正在提供Javascript,所以如果content-type为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: 我从来没有处理过,但是如果这是您的问题,文档确实会说jQuery 1.5及更高版本中有一个解决方案:

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. 多个用空格分隔的值:从jQuery 1.5开始,jQuery可以将dataType从在Content-Type标头中接收的dataType转换为所需的dataType。 For example, if you want a text response to be treated as XML, use "text xml" for the dataType. 例如,如果您希望将文本响应视为XML,请对数据类型使用“文本xml”。 You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." 您还可以发出JSONP请求,将其接收为文本,并由jQuery解释为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. 同样,速记字符串(例如“ jsonp xml”)将首先尝试从jsonp转换为xml,然后,尝试从jsonp转换为文本,然后从文本转换为xml。

via http://api.jquery.com/jQuery.ajax/ (see the doc for dataType) 通过http://api.jquery.com/jQuery.ajax/ (有关数据类型,请参阅文档)

That means you can possibly use this: dataType: "script text" to have a Javascript response interpreted as text. 这意味着您可以使用以下命令: dataType: "script text"将Javascript响应解释为文本。 Of course, you could also just use dataType: "script" to have it interpreted as script. 当然,您也可以只使用dataType: "script"将其解释为脚本。

Ended up writing my own xhr call (sans error-handling) to allow this. 最终写了我自己的xhr调用(没有错误处理)以允许这样做。 Terrible solution to a terrible problem. 解决一个可怕问题的好方法。 (Double fail for the eval()). (两次对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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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