Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jQuery solution that uses CORS when available and falls back to XDomainRequest on MSIE and JSONP on browsers with niether?

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?

like image 844
hippietrail Avatar asked Dec 31 '11 14:12

hippietrail


2 Answers

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.

like image 125
monsur Avatar answered Nov 18 '22 18:11

monsur


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!

like image 3
SD05 Avatar answered Nov 18 '22 17:11

SD05