1

I can't seem to be able to get XMLHttpRequest() to work cross domain. I am able to get it to work cross domain via XDomainRequest() but I need to do a synchronous request.

 var url = 'http://phpfilethatspitsoutjson.com';

 // Can't get this to work
 var req = new XMLHttpRequest();
 req.open("GET", url, false);
 req.send(null);

 // This does work
 xdr = new XDomainRequest();   // Creates a new XDR object.
 xdr.open("GET", url); // Creates a cross-domain connection with our target    server using GET method. 
 xdr.send(); //Send string data to server
 xdr.onload = function () {
 };

Also I do have header("Access-Control-Allow-Origin: *"); set in my url.

albertski
  • 2,428
  • 1
  • 25
  • 44
  • 1
    There's plenty of information online about whether or not you can use XMLHttpRequest cross domain. Just look at the ***Related*** list to the right for starters. And why do you think you need a *synchronous* request? – cookie monster May 23 '14 at 02:16
  • and do you really need to do a synchronous request? That is very user-unfriendly. – Thilo May 23 '14 at 02:16
  • 1
    http://enable-cors.org – Charlie May 23 '14 at 02:30
  • What are you not using jquery ? That will surely help you. – Madhurendra Sachan May 23 '14 at 02:31
  • If you are pulling in JSON data and it is not dynamic, more of a once off, you could just use a standard in the HTML. – silver May 23 '14 at 02:40
  • @MADTerry: How is jQuery going to help make a cross-domain XHR request? – cookie monster May 23 '14 at 02:43
  • @cookiemonster - jQuery.getJSON()... http://api.jquery.com/jquery.getjson/ – dana May 23 '14 at 02:45
  • @dana: Yes? How will that help with making a cross-domain XHR request? jQuery can't overcome Same-Origin-Policy violations any better than native code can. – cookie monster May 23 '14 at 02:46
  • If you use it in `JSONP` mode, (with a `callback=?` parameter), it should work. I believe it injects a ` – dana May 23 '14 at 02:54
  • @dana Only if the server returns the callback with the JSON data – Charlie May 23 '14 at 03:43
  • @cookie monster I need it to be synchronous because I want to modify my html based on what I get back from my josn before the entire page loads. – albertski May 23 '14 at 13:12
  • @MADTerry I actually am using JQuery but $.ajax doesn't work for IE8. – albertski May 23 '14 at 13:13
  • @dana: Yes, if the server can send JSONP, but one certainly doesn't need jQuery to do that. It's very easy with native code. – cookie monster May 23 '14 at 17:57
  • 1
    @albertski: So your actual question is why your `$.ajax` request doesn't work in IE8 but does in other browsers. – cookie monster May 23 '14 at 17:58
  • @cookie monster Just trying to find out if it is possible to do cross domain requests via XMLHttpRequest(). I already know that $.ajax won't work in IE8. – albertski May 25 '14 at 15:37
  • @albertski: `$.ajax` works just fine in IE8 unless perhaps you're using jQuery 2.0 or later, which you shouldn't be if you need IE8 support. But in jQuery 1.x, it works fine, so if you're having an issue, you should be asking about that. – cookie monster May 25 '14 at 17:34

0 Answers0