Is there a jQuery solution that uses CORS when ava

2019-02-12 16:03发布

问题:

I was silly enough to believe that all modern browsers supported CORS (Cross-Origin Resource Sharing).

When my toy JavaScript tool didn't work on IE9 or Opera I found out that the former only supported CORS through XDomainRequest, which jQuery doesn't seem to support. Opera doesn't support CORS at all yet.

Now jQuery has an abstract interface that does normal same domain AJAX queries, CORS queries, and even JSONP queries the same way.

Is there an extension to this interface that goes further making it work even with XDomainRequest and falling back to JSONP when CORS isn't available?

回答1:

Here is a plugin for XDomainRequest support in JQuery:

https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js

There is also a jQuery feature request to add this feature:

http://bugs.jquery.com/ticket/8283

Note that the bug report says that there may be an issue with the plugin in IE9, so YMMV. It ended up getting closed with the reason given that the plugin exists to do the job.



回答2:

Take a look at xReader:

<script src="http://kincrew.github.com/xReader/xReader.full.js"></script> 
<script type="text/javascript"> 
xReader("http://www.yahoo.com/", function(data) { 
    alert(data.content); 
})
</script>

It's simple!