且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-01 11:29:22

查看 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.