javascript Jquery ajax() 跨域远程服务器在 IE8 中不起作用

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

Jquery ajax() cross domain remote server does not work in IE8

javascriptjqueryajaxinternet-explorer-8cross-domain

提问by

I have a script that makes an ajax request to a remote server, that returns a plain text response. It works fine in all browsers except IE8 (shocker).

我有一个脚本向远程服务器发出 ajax 请求,返回纯文本响应。它在除 IE8 (shocker) 之外的所有浏览器中都能正常工作。

Here's the code:

这是代码:

$.ajax({
  url: 'abc.com/?somerequest=somevalue',
  cache: false,
  type: 'POST',
  data:{
    sub: 'uploadprogress',
    uploadid: this.uploadId
  },
  dataType: 'html',
  success: this.uploadProgressResp,
  error: this.errorResp
});

In IE8, it returns a "No Transport" error. I suppose it's because IE8 doesn't allow cross domain requests?

在 IE8 中,它返回“无传输”错误。我想这是因为 IE8 不允许跨域请求?

NOTE:I didn't write the API for the remote server. If I did, I would return JSON response rather than a plain text response. So yes, the dataType is supposed to be HTML rather than JSON.

注意:我没有为远程服务器编写 API。如果我这样做了,我会返回 JSON 响应而不是纯文本响应。所以是的,dataType 应该是 HTML 而不是 JSON。

回答by SpaceBison

Try adding this somewhere before the ajax call- Bestplace for it is before any other JavaScript executes!

尝试在ajax 调用之前的某个地方添加它-最好的地方是在任何其他 JavaScript 执行之前!

jQuery.support.cors = true;

Without this, the "No transport" error will be thrown by Internet Explorer. The error message itself is rather confusing, but by default cross-domain ajax requests are blocked by IE, but do not appear to be so by other browsers - or at least, Chrome and Firefox will function to that effect.

如果没有这个,Internet Explorer 将抛出“No transport”错误。错误消息本身相当令人困惑,但默认情况下,跨域 ajax 请求会被 IE 阻止,但其他浏览器似乎不会这样做 - 或者至少,Chrome 和 Firefox 会起到这种作用。

I shared your pain on this one, historically. Quite confident that it will sort your issue.

从历史上看,我分享了您对此的痛苦。非常有信心它会解决您的问题。

回答by Nicolo

I know this is very old question, but sadly people still using IE8/9 and sometimes we have to support them :/

我知道这是一个很老的问题,但遗憾的是人们仍在使用 IE8/9,有时我们必须支持他们:/

This is best solution I was able to find for this issue:

这是我为这个问题找到的最佳解决方案:

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

Just include following script in your html and that's it, you don't have to modify anything in your jQuery request

只需在您的 html 中包含以下脚本即可,您无需在 jQuery 请求中修改任何内容

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js"></script>

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js"></script>

Limitations:

限制:

  • IE6/7 isn't supported, only IE8 and IE9
  • Minimum jQuery version is 1.5
  • When using POST method in IE8/9, Content-Typeheader always will be set to text/plain
  • Current website and requested URL both must be using same protocol (HTTP->HTTPS or HTTPS->HTTP requests will not work)
  • 不支持 IE6/7,仅支持 IE8 和 IE9
  • 最低 jQuery 版本为 1.5
  • 在 IE8/9 中使用 POST 方法时,Content-Typeheader 总是会被设置为text/plain
  • 当前网站和请求的 URL 都必须使用相同的协议(HTTP->HTTPS 或 HTTPS->HTTP 请求将不起作用)