Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with jquery ajax cross domain xml response

Tags:

jquery

ajax

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

like image 982
Murali N Avatar asked Jan 15 '23 09:01

Murali N


1 Answers

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.

like image 145
T.J. Crowder Avatar answered Jan 31 '23 07:01

T.J. Crowder