Murali N
Murali N

Reputation: 3498

issue with jquery ajax cross domain xml response

Here is my code to access xml from a website

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://rxnav.nlm.nih.gov/REST/Ndfrt/search?conceptName=TESTOSTERONE",
        dataType: "xml",
        success: xmlParser

      });
});

function xmlParser(xml) { 
    $(xml).find("entry").each(function () {
        $(".entirecont").append($(this).find('inputConceptName').text());
    });
}

it's working fine in local when i push this code to production it's giving me the cross domain restrictions.

Here is the JsFiddle

I know it's a cross domain request but, how can i fix it??

Thanks

Upvotes: 4

Views: 8178

Answers (4)

zdesam
zdesam

Reputation: 2976

You can use flxhr from https://github.com/flensed/flXHR to run cross domain ajax call

Sample Code to use

function crossDomainCall(){
    var flproxy = new flensed.flXHR({
        autoUpdatePlayer: true,
        instanceId: "myproxy1",
        onerror: handleError,
        onreadystatechange: handleCrossDomainCall
    });
    flproxy.open("POST", url);
    flproxy.send(null);
}

function handleCrossDomainCall(XHRobj){
    if (XHRobj.readyState == 4) {
        var xmlDoc = XHRobj.responseXML;
        //
    }
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074295

With XML, your only real option for a true cross-domain request is if that server supports CORS, allows your origin, and your browser supports it. (If they have a JSONP option, though, that would be easier. Sadly, though, a quick look at their API page suggested they only support XML and JSON, not JSONP. But look for yourself, don't take my word for it, I didn't do a detailed read. It's slightly odd if they support JSON but not JSONP, in my view.)

Another option I've sometimes heard discussed but have done is using YQL as a cross-domain proxy.

Of course, you can also run your own server, make the requests to it, and have it query the rxnav.nlm.nih.gov feed and return it to you. Then the SOP doesn't come into it.

Side note: To use CORS with jQuery in IE8 or IE9, you need a plug-in that handles using the special XDomainRequest object (IE8 and IE9's XMLHttpRequest object doesn't do CORS). IE10 finally fixes that.

Upvotes: 9

Kasyx
Kasyx

Reputation: 3200

You cannot make cross domain request via ajax (expect json).

I suggest you make local ajax request, and on server side connect to cross domain server to get data which you need.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can only use CORS or JSONP in a cross domain request. An AJAX request to fetch XML will not work cross domain

The workaround is to create a server-side proxy on your local server using PHP/ASP.Net/language of choice and call that via AJAX instead.

Upvotes: 0

Related Questions