Frage

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!

War es hilfreich?

Lösung 2

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!

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top