跨域 Ajax 请求在 Opera 和 IE9 中不起作用?

2022-01-15 00:00:00 cors jquery javascript ajax cross-domain

我正在使用这个页面 - http://ecmazing.com/cors.html - 制作对此资源的跨域 Ajax 请求:http://hacheck.tel.fer.hr/xml.pl

它适用于 Chrome、Safari 和 Firefox,但不适用于 IE9 和 Opera.

代码:

var pdata = {'textarea': 'test'};$.post('http://hacheck.tel.fer.hr/xml.pl', pdata, function(data, status, xhr) {output.value = xhr.responseText;});

(预期结果是一个 XML 代码字符串.)

自己看看: http://ecmazing.com/cors.html

在 IE9 和 Opera 中,XHR 对象的 error 处理程序执行并传入此错误对象:

<代码>{就绪状态:4,状态:0,状态文本:'错误'}

如您所见,这个错误对象并没有透露太多信息.

如何让它在 IE9 和 Opera 中运行?

解决方案

查看 cors at whencaniuse 的条目.p>

对于 Internet Explorer,CORS 是在 IE8 和 IE9 中使用 XDomainRequest 对象支持",因此您需要使用备用对象来使用它.

Opera 根本不支持它.

如果你在 Opera 中需要跨域 Ajax,那就使用 JSON-P.

I am using this page - http://ecmazing.com/cors.html - to make a cross-origin Ajax request to this resource: http://hacheck.tel.fer.hr/xml.pl

It works in Chrome, Safari and Firefox, but doesn't in IE9 and Opera.

The code:

var pdata = {'textarea': 'test'};

$.post('http://hacheck.tel.fer.hr/xml.pl', pdata, function(data, status, xhr) {
    output.value = xhr.responseText;
});

(The expected result is an XML code string.)

See for yourself: http://ecmazing.com/cors.html

In IE9 and Opera, the error handler of the XHR object executes and this error object is passed in:

{
    readyState: 4,
    status: 0,
    statusText: 'error'
}

As you can see, this error object doesn't reveal much information.

How can I make it work in IE9 and Opera?

解决方案

See the entry for cors at whencaniuse.

For Internet Explorer CORS is "Supported somewhat in IE8 and IE9 using the XDomainRequest object", so you need to use an alternate object to use it.

Opera simply doesn't support it.

If you need cross-domain Ajax in Opera, then use JSON-P.

相关文章