为什么这个 jQuery Ajax 调用只在 IE9 中失败(甚至在 IE8 和 IE7 中也能正常工作)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6905911/
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 21:32:10  来源:igfitidea点击:

Why does this jQuery Ajax call fail ONLY in IE9 (Even works fine in IE8 and IE7)

jqueryajaxinternet-explorer-9

提问by rushinge

I have a website where I make an ajax call like this:

我有一个网站,我可以在其中进行这样的 ajax 调用:

            // perform an ajax request to generate a new subscriber account
            jQuery.ajax({
                type: 'POST',
                url: '/index.php?option=com_content&view=article&id=45&tmpl=component',
                data: postVars,
                success: handleResponse,
                error: function(jqXHR, textStatus, errorThrown) {
                    alert ('response: ' + jqXHR.responseText);
                    alert ('code: ' + jqXHR.getResponseHeader('X-Subscriber-Status'));
                }
            });

The page being requested doesn't do anything but return a single digit status code and it works fine with every browser except for IE9. The script is hosted on the same domain as the page it's requesting so there shouldn't be any issue with cross domain scripting.

被请求的页面除了返回一位数的状态代码之外什么都不做,它适用于除 IE9 之外的所有浏览器。该脚本与其请求的页面托管在同一域中,因此跨域脚本编写应该没有任何问题。

I finally tracked down what's happening but I don't know how to fix it. It turns out that the Ajax request completes successfully with no problems. I verified this by using Fiddler to look a the network traffic. The server responds to the request with an HTTP status code of 200 and the body doesn't contain anything but a single digit response code. At this point jQuery executes the error handler, NOT the success handler as would be expected (in IE9). Why does it do this and how can I prevent it?? This ONLY happens in IE9. Even IE8 and IE7 work just fine with the exact same code! As you can see I finally resorted to detecting IE9 and using an XmlHttpRequest object to do it (which works just fine by the way).

我终于找到了正在发生的事情,但我不知道如何解决。事实证明,Ajax 请求成功完成,没有任何问题。我通过使用 Fiddler 查看网络流量来验证这一点。服务器使用 200 的 HTTP 状态代码响应请求,并且正文不包含任何内容,只包含一个数字响应代码。此时 jQuery 执行错误处理程序,而不是预期的成功处理程序(在 IE9 中)。为什么会这样,我该如何预防?这仅发生在 IE9 中。即使 IE8 和 IE7 使用完全相同的代码也能正常工作!正如你所看到的,我最终求助于检测 IE9 并使用 XmlHttpRequest 对象来完成它(顺便说一下,它工作得很好)。

This seems like a jQuery bug to me but I can't find any mention of it in my searches. Am I really the only one experiencing this odd behavior?

这对我来说似乎是一个 jQuery 错误,但我在搜索中找不到任何提及。我真的是唯一一个遇到这种奇怪行为的人吗?

采纳答案by rushinge

Well I finally tracked down the problem. It turns out that for some reason jQuery/IE does not correctly urlencode double quotes. The URL of the request was:

好吧,我终于找到了问题所在。事实证明,由于某种原因,jQuery/IE 没有正确地对双引号进行 urlencode。请求的 URL 是:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks OR "stock market" -blueprint -empireavenue.com -learn&_=1313510362469

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks 或“股票市场” -blueprint -empireavenue.com -learn&_=1313510362469

In every other browser by the time jQuery performed the ajax request it looked like:

当 jQuery 执行 ajax 请求时,在所有其他浏览器中,它看起来像:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks%20OR%20%22stock%20market%22%20-blueprint%20-empireavenue.com%20-learn&_=1313510362469

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks%20OR%20%22stock%20market%22%20-blueprint%20-empireavenue.com%20-learn&_=1362456103

but for whatever reason in all versions of IE it came out like this:

但无论出于何种原因,在所有版本的 IE 中,结果都是这样的:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks%20OR%20"stock%20market"%20-blueprint%20-empireavenue.com%20-learn&_=1313510362469

/search.json?callback=jQuery161052344487927080267_1313510362362&q=stocks%20OR%20"stock%20market"%20-blueprint%20-empireavenue.com%20-learn&_=131359103624

which caused the server to return no data.

这导致服务器不返回任何数据。

回答by chris5marsh

Are you sure that the URL you're calling (/index.php?option=com_content&view=article&id=45&tmpl=component) works in IE9? If you load that PHP page up in IE9, does it return the expected result? It should do, but it's worth checking that the error is in the jQuery call, rather than the PHP.

您确定您正在调用的 URL ( /index.php?option=com_content&view=article&id=45&tmpl=component) 在 IE9 中有效吗?如果您在 IE9 中加载该 PHP 页面,它是否返回预期结果?它应该可以,但值得检查错误是在 jQuery 调用中,而不是在 PHP 中。

Also, a POST call would usually be to a page like 'index.php', with the query string (option=com_content, view=articleetc.) sent as the variable postVars.

另外,后调用通常会是像“的index.php”的网页,查询字符串(option=com_contentview=article发送作为变量等)postVars

Try using the following:

尝试使用以下方法:

$.ajax({
    type: "POST",
    url: "index.php",
    data: {
        option : com_content,
        view : article,
        id : 45,
        tmpl : component
    },
    success: function(msg){
        console.log(msg);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus)
    }
});

Strip down the function to its basic parts, and you should be able to see where the error is coming from.

将函数分解为其基本部分,您应该能够看到错误的来源。