I am doing a cross domain request using $.ajax
. It works on Firefox and Chrome, but does not issue a call on IE 7 or 8. Can anyone tell me what's wrong with the following?
- I have used JSON and JSONP (which I stopped using, due to some custom restrictions).
- I am already using
Allow-access-control-origin
header on my site. (Without those, Chrome and Firefox were not making successful requests.) - I have already tried https://developer.mozilla.org/en/http_access_control
Code:
$.ajax({
type: 'GET',
url: "http://anotherdomain.com/Service/GetControl?id=" + zoneID,
cache: false,
contentType: "application/x-www-form-urlencoded",
async: false,
beforeSend: function (request) {
//alert('before send');
//request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//request.setRequestHeader("X-PINGOTHER", "pingpong");
} ,
success: function (data, status) {
//alert("Data returned :" + data);
//alert("Status :" + status);
if (status == "success" && data != "")
$("#" + div.id).append(data);
else
$("#" + div.id).attr("style", "display:none;");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
}
});
I have tried various tips present on multiple sites, but no luck yet.
I have the same problem in IE, I solved it by replacing:
To
So basically upgrade your jquery version.
Could you check if the problem with IE relies on not defining security zones to allow cross domain requests? See this microsoft page for an explanation.
OTOH, this page mentions that IE7 and eariler cannot do cross domain calls, but IE8 can, using a different object than XMLHttpRequest, the one JQuery uses. Could you check if XDomainRequest works?
EDIT (2013-08-22)
The second link is dead, so I'm writing here some of its information, taken from the wayback machine:
Simply add "?callback=?" (or "&callback=?") to your url:
When doing the calls (with everything else set properly for cross-domain, as above) this will trigger the proper JSONP formatting.
More in-depth explanation can be found in the answer here.
Microsoft always ploughs a self-defeating (at least in IE) furrow:
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
CORS works with XDomainRequest in IE8. But IE 8 does not support Preflighted or Credentialed Requests while Firefox 3.5+, Safari 4+, and Chrome all support such requests.
Note, adding
was sufficient to force $.ajax calls to work on IE8
It's hard to tell due to the lack of formatting in the question, but I think I see two issues with the ajax call.
1) the application/x-www-form-urlencoded for contentType should be in quotes
2) There should be a comma separating the contentType and async parameters.