0

Using jquery-1.9.1 , I have enabled CORS support.

$.support.cors = true;

Then I am making an ajax request as shown below, I have also enabled jsonp callback support from the server side.

$.ajax({
            type: 'GET',
            url: url,
            async: false,
            contentType: "application/json",
            jsonpCallback: 'jsonCallback',
            dataType: 'json',
            success: function(data) {
               var featureJSON = data.feature;

            },
            error: function(e) {

            },
            complete: function () {

            }
        });

I have deployed this in jboss and accessed the page from another computer(different domain/ip address ) . This ajax call works in FF and Chrome from another computer and in the same computer.

It also works in IE , but in the same computer.

Will not work in IE when accessed from some other computer other than the one I deployed in.

After googling around , I understood that IE* do not allow cross domain ajax calls which is made by jquery.And jquery do not support IE's XDomain object.

To conclude , how do I make jquery use IE's XDomain object ? , I have almost completed the app ( my bad , I didnt check the XDomain thing ) . I cannot rebuild the code again. Is there any other solution ?

Tito
  • 8,894
  • 12
  • 52
  • 86
  • 1
    jsonp is not used with CORS, it's an alternative to it – David Fregoli Mar 21 '13 at 13:06
  • @SaurabhBayani I have tried XDomainRequest , as mentioned here http://blog.mycila.com/2011/07/jquery-cors-plugin.html , but no luck , should I add CORS support in response header from the server side Access-Control-Allow-Origin","*" ? – Tito Mar 21 '13 at 13:18
  • Have a look, this might be helpful http://stackoverflow.com/questions/3362474/jquery-ajax-fails-in-ie-on-cross-domain-calls – Saurabh Bayani Mar 21 '13 at 13:18
  • @SaurabhBayani checked the link , seems like I have to write separate code for IE8 , like I said , I already finished my app , cant rewrite it for IE.Any other plugin which will extend jquery's $.ajax calls ? – Tito Mar 21 '13 at 13:34
  • IE8 and 9 do not support CORS, you have to use jsonp for them – David Fregoli Mar 21 '13 at 13:45
  • @DavidFregoli doesnt work either way.It has to do with the XDomainRequest object for IE8 , it works in IE10 !. I can rewrite the code specifically for IE8 , but looking for a plugin which can extend the $.ajax by checking the browser type. – Tito Mar 21 '13 at 13:58
  • Expanding on David Fregoli's response, IE does not even sport a 'cors' property in the 'supports' property, so...there isn't even an opportunity to set that property, even if it were read/write. I still have to manipulate the request headers manually to get CORS working when supporting IE. The HTML5 compatibility of IE 10 is the lowest common denominator in my development cycle and Chrome is the least bothersome. I should have a suitable javascript function completed by the end of the day that will handle the main browsers I focus on...IE, Chrome, Safari and Webkit. – jinzai Jun 17 '13 at 18:43

1 Answers1

2

you can't "enable" CORS...$.support is supposed to be a ready only table of what the current browser supports. Changing false values to true doesn't magically add unsupported features to old browsers!

if ($.support.cors) {
// your code
} else {
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        data: {
          callback : '?'
        },
        success: function(data) {
           var featureJSON = data.feature;

        },
        error: function(e) {

        },
        complete: function () {

        }
   });
}
David Fregoli
  • 3,377
  • 1
  • 19
  • 40