XDomainRequest problem
Asked Answered
C

4

8

I'm trying to make a asynchronous call to a service that returns json using XDomainRequest (IE8). The problem is that i always get an error (the onerror event is fired, and the responseText is always null), i'm using fiddler to check the response of the service and i seems right (I can see the json object returnig), this only happen in IE8 when using XDomainRequest, the same functionality implemented in JQuery works fine.

Any clue would be appreciated. Thanks!

P.S.: This is my javascript code:

.....
  if (jQuery.browser.msie && window.XDomainRequest) {
    //Use Microsoft XDR
    var xdr = new XDomainRequest();
    xdr.open("post", url);
    xdr.onload = function () {
       alert("Loading");
       alert(xdr.responseText);
    };
    xdr.onsuccess = function() {
       alert("Success!");
       alert(xdr.responseText);
    };
    xdr.onerror = function() {
       alert("Error!");
       alert(xdr.responseText);
    };
    xdr.onprogress = function() {
       alert("Progress");
       alert(xdr.responseText);
    };
    xdr.timeout = 1000;
    xdr.send("data: " + escape(data));
    var response = xdr.responseText;
 } else .....
Colburn answered 19/1, 2011 at 18:52 Comment(0)
K
8

Are you sure that the service is sending a Access-Control-Allow-Origin-header matching the requesting URL?

Kyles answered 19/1, 2011 at 21:6 Comment(0)
P
7

Your problem may be the content-type sent, because XDomainRequest only support "text/plain".

Reference: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

Cheers,

Andre Pedroso

Pundit answered 9/2, 2011 at 17:40 Comment(0)
B
4

A year old post,, you still there GiaNU?! You are calling xdr.onsuccess but I don't think that method exists. The xdr.onload works and seem to be equivalent to jQuery's AJAX "success" function.

This X-Domain stuff is quite new but there is a very nice working model available from MS now here: AJAX - Introducing Cross-domain Request (XDR)

The xdr.ontimeout I can't get to do a thing, but don't find a need yet :) I got things up and running first w/jQuery and now with ie9 thank's to the MS post.

The XDR has some trouble with the timing for my current application and is just used a timeout to handle it:

xdr.onload = setTimeout( function(){ doIt( xdr.responseText ), 2000});
Bellicose answered 27/3, 2012 at 23:33 Comment(1)
setTimeout won't help. The actual problem is that IE8 can garbage collect XDomainRequest while it has a pending request open. If you look at your setTimeout call, you're capturing the XDR variable which avoids the garbage collector for slightly longer - just moves the problem. I posted a detailed answer with workarounds here: #8058946Argot
L
1

Another gotcha is if you're running the service via Cassini then the "Access-Control-Allow-Origin" header won't be returned as Cassini doesn't recognise this. We had a scenario where our service calls were working on a test server but not working locally. Turns out the service was hosted in Cassini on our local dev machine but hosted on IIS on the test server.

Also here's the web.config setting for anyone that needs it (note: this allows access from any domain - "*"):

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
Ladysmith answered 11/5, 2012 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.