3

I've a problem...I use jQuery ajax to call a web service that returns XML. The jQuery ajax stuff works awesome for every browser except for ie.

So for ie browsers, I am using XDomainRequest. Here is the code:

if ($.browser.msie && window.XDomainRequest) {
    // Use Microsoft XDR
    var xdr = new XDomainRequest();
    xdr.open("get", theUserUrl);
    xdr.timeout = 95000;
    xdr.onerror = function () {
        console.log('we have an error!');
    }
    xdr.onprogress = function () {
        console.log('this sucks!');
    };
    xdr.ontimeout = function () {
        console.log('it timed out!');
    };
    xdr.onopen = function () {
        console.log('we open the xdomainrequest');
    };
    xdr.onload = function () {
        // XDomainRequest doesn't provide responseXml, so if you need it:
        var xml2 = new ActiveXObject("Microsoft.XMLDOM");
        xml2.async = false;
        xml2.loadXML(xdr.responseText);
        console.log('do we get any response text at all?: ' + xdr.responseText);
        ParseOwnershipObjects(xml2);
        //AddServiceRequestsToMap(xml2, map, spinner);
    };
    xdr.send();
}
  • This exact code works fine elsewhere in the application with a different url.

    The url is fine, it returns exactly what it should in the browser (and hence why the jquery ajax call works). Couple of things to note:

    I am integrating my own html/javascript with another guy's asp.net project.

In the global.asax.cs file, I have:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,OPTIONS");
}

so I don't think that it's a header problem.

None of my handlers fire. Not the onprogress, ontimeout, onerror...nothing! I don't have time to convert the web service to JSON.

Any thoughts?

Thanks!

jdb1a1
  • 1,045
  • 1
  • 13
  • 32

2 Answers2

2

Disclaimer - I actually haven't used 'XDomainRequest' - when using jQ I set data to jsonp for xdomain requests...

When debugging - are you using IE Dev tools (F12)? If not, the error is likely console.log

EDIT: mea culpa, disregard the jsonp stuff - missed the part you mentioned XML


Update:

Out of curiosity I'm trying XDomainRequest. I copied your code and just added a value for theUserUrl.

  • as above/expected, unless I have Internet Explorer Developer tools running, console is undefined - and may give the impression that "none of your handlers are firing".

  • Once I have the IE dev tools enabled (docked or otherwise) xdr.onerror fires. we have an error is logged in the IE console. So while there is an error, the handler does fire.

Internet Explorer Dev Tools Consoloe

A quick read on XDomainRequest requires the responding server to have the Access-Control-Allow-Origin header. I'm calling my own server and I know I don't have this header set, so without further debugging, it would be a good guess that's why xdr.onerror is being fired.

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • Thanks for the response. I have been using F12, specifically the console. When I leave the dev tools undocked in the window, whenever I open the test page (i.e. http://localhost:28730/Test.aspx), it always crashes the dev tools (in that it closes the window). When I dock the dev tools into the browser window, I don't get any output. I also commented out all off the console.log commands to no avail. – jdb1a1 May 16 '12 at 13:54
  • Thanks. I don't think that the console had anything to do with it, because I commented them out multiple times and was always running the developer tools. Thanks for the help, though. I appreciate it! – jdb1a1 May 16 '12 at 19:37
1

As it turns out, there were special characters in the url parameters that were not being correctly dealt with by the XDomainRequest object. Instead of the GET request, I am going to use the POST request on internet explorer-only queries.

EDIT - I ended up switching the web service over to return output in JSON format, thus negating the need for the XDomainRequest. Using JSON speeds things up a bit too, I recommend it!

jdb1a1
  • 1,045
  • 1
  • 13
  • 32
  • Can you clarify how changing the response format negated the need for XDomainRequest? It's still cross-domain, correct? – Seth Bro Aug 22 '12 at 18:08
  • Using JSON allows you to circumvent use of the XDomainRequest object for IE-specific cases, and use jQuery ajax for everything. The cross-domain problem disappears. Check out this answer for more details on using JSON with jquery Ajax: http://stackoverflow.com/a/8894013/575861 – jdb1a1 Aug 22 '12 at 19:12