jQuery chrome中的jquery ajax问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1742049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:04:48  来源:igfitidea点击:

jquery ajax problem in chrome

jqueryajaxgoogle-chrome

提问by spaceman

i have the following jquery code running on my page just fine in FF and IE, but chrome seems to be freaking out..

我在我的页面上运行了以下 jquery 代码,在 FF 和 IE 中运行良好,但 chrome 似乎吓坏了..

in FF and IE the call is made and the result is appended to the div. in chrome, it calls ajaxfailed on failure.

在 FF 和 IE 中进行调用并将结果附加到 div。在 chrome 中,它在失败时调用 ajaxfailed。

the XMLHttpRequest passed to the AjaxFailed function has a status code of "200" and the statusText is "ok". the readystate is 4 and the responseText is set to the data i wish to append to the div.. basically from what i can see its calling the failure method but it isn't failing.. i have tried with both get and post requests and it always breaks in chrome.

传递给 AjaxFailed 函数的 XMLHttpRequest 的状态代码为“200”,statusText 为“ok”。readystate 为 4 并且 responseText 设置为我希望附加到 div 的数据..基本上从我可以看到它调用失败方法但它没有失败..它总是在铬中断。

function getBranchDetails(contactID, branchID) {
  $.ajax({
    type: "GET",
    url: urlToRequestTo,
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: branchDetailsSuccess,
    error: AjaxFailed
  });
}



 function branchDetailsSuccess(result) {
      $("#divBranchControl").empty();
      $("#divBranchControl").append(" " + result);
      $("#branchDiv").tabs();
    }



 function AjaxFailed(result) {
      alert("FAILED : " + result.status + ' ' + result.statusText);
    }

采纳答案by spaceman

I just saw that this question has gotten a lot of views, and its still open. I'd forgotten about it completely and hopefully this will let me close it.

刚刚看到这个问题已经有很多意见了,而且还是开放的。我已经完全忘记了它,希望这能让我关闭它。

Setting the datatype argument to nothing, or even removing the datatype argument completely will solve the problem.

将数据类型参数设置为空,甚至完全删除数据类型参数都可以解决问题。

In my example I am returning a rendered view (an html snippet in string) and in this code I'm specifying the data type to json, when really it isn't. Most other browsers seem to ignore the datatype if its incorrect and move on with life, allowing me to append the html result.

在我的示例中,我返回一个渲染视图(字符串中的 html 片段),在此代码中,我将数据类型指定为 json,而实际上并非如此。如果数据类型不正确,大多数其他浏览器似乎会忽略该数据类型并继续前进,允许我附加 html 结果。

Chrome throws an error. The status text is OK, the status code is 200, because the actual ajax request went through fine. The problem has nothing to do with the request itself, the problem is that the data returned is not what I told chrome it would be.

Chrome 会引发错误。状态文本是可以的,状态码是 200,因为实际的 ajax 请求通过得很好。问题与请求本身无关,问题是返回的数据不是我告诉 chrome 的那样。

So chrome breaks. If I remove the datatype argument completely chrome figures out what the data is when it gets it. If I set the datatype argument to "html" then it also works fine.

所以铬断裂。如果我完全删除数据类型参数,chrome 会在获取数据时弄清楚数据是什么。如果我将数据类型参数设置为“html”,那么它也可以正常工作。

Long story short, the problem is not chrome. Its me. Because I'm dumb like that. I am marking this as the answer to this question as it answers the example I presented in the original question.

长话短说,问题不在于铬。这就是我。因为我就是那样笨。我将其标记为这个问题的答案,因为它回答了我在原始问题中提出的示例。

In the comments others have described other situations where this solution is most likely not going to help.

在评论中,其他人描述了此解决方案很可能无济于事的其他情况。

回答by MSS

In the AJAX operation just add: async: falseafter datatype: "json", and that should solve your problem. Chrome has issue handling asynchronous calls.

在 AJAX 操作中只需添加: async: falseafter datatype: "json",这应该可以解决您的问题。Chrome 在处理异步调用时遇到问题。

回答by Krokador

I don't know if you still have this issue, but I ran into a similar error just today. I was calling an aspx page to return a string with responseText and Chrome never returned anything. Turned out I had a Response.Close in my aspx page which worked everywhere else but probably didn't send some required headers or something to Chrome and/or Safari. Hope that helps someone.

我不知道你是否还有这个问题,但我今天遇到了类似的错误。我正在调用一个 aspx 页面以返回一个带有 responseText 的字符串,而 Chrome 从未返回任何内容。结果我在我的 aspx 页面中有一个 Response.Close ,它可以在其他任何地方工作,但可能没有向 Chrome 和/或 Safari 发送一些必需的标头或其他内容。希望能帮助某人。

回答by spaceman

after a day and a half i got over it, so may i present.....

一天半后我克服了它,所以我可以介绍......

function getBranchDetails(contactID, branchID) {

  $.ajax({
    type: "GET",
    url: urlToRequestTo,
    data: "",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: branchDetailsSuccess,
    error: branchAjaxFailed
  });
}

function branchDetailsSuccess(result) {
  $("#divBranchControl").empty();
  $("#divBranchControl").append(" " + result);
  $("#branchDiv").tabs();
}

function branchAjaxFailed(result) {
  if (result.status == 200 && result.statusText == "OK") {
    //this is here only because chrome breaks on this method only for no reason whatsoever.
    //chrome sees the request as failed, but everything happens fine...
    branchDetailsSuccess(result.responseText);
  }
  else {
    alert("FAILED : " + result.status + ' ' + result.statusText);
  }
}

回答by Lobstrosity

Try setting the data parameter to "".

尝试将数据参数设置为“”。

For GETrequests, the data parameter is appended to the URL. Not sure why Chrome would have an issue with that but it's worth a shot :)

对于GET请求,数据参数附加到 URL。不知道为什么 Chrome 会遇到这个问题,但值得一试:)

回答by Sam Malayek

I just ran into this in Chrome (was fine in Firefox) and adding async: trueto the ajax parameters solved it.

我刚刚在 Chrome 中遇到了这个问题(在 Firefox 中很好)并添加async: true到 ajax 参数解决了它。

回答by Jimmeh

Try this for success :

试试这个成功:

success: function(response) { branchDetailsSuccess(response); },

回答by NAC

Use POST method in servlet and change type:POSTin $.ajax.

在 servlet 中使用 POST 方法并type:POST$.ajax.