Jquery ajax cross domain xml response issue

Here is my code to access xml

from the 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 works fine in local mode when i click this code to build giving me cross domain restrictions.

Here is a JsFiddle

I know this is a cross domain request, but how can I fix it?

thank

+3


source to share


4 answers


With XML, your only viable option for a genuine cross-domain request is if this server supports CORS , lets you specify your origin and your browser supports it. (If they have the JSONP option it would be easier. Unfortunately, however, a quick glance at their API page suggested that they only support XML and JSON, not JSONP, but look yourself, don't take my word for it, I don't did a detailed read. A bit weird if they support JSON but not JSONP in my opinion.)

Another option that I sometimes heard discussed, but did using YQL as a cross-domain proxy .



Of course, you can also run your own server, request it and request the rxnav.nlm.nih.gov feed from it and return it to you. Then SOP is not included in it.

Note. To use CORS with jQuery in IE8 or IE9, you need a plugin that handles a custom object XDomainRequest

(IE8 and IE9 object XMLHttpRequest

does not enforce CORS). Finally, IE10 fixes this.

+9


source


You can use CORS

or JSONP

in a cross-domain query. AJAX request to retrieve XML won't work cross domain



A workaround is to create a proxy on the local server using PHP / ASP.Net / language of choice and call that instead of AJAX.

0


source


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

I suggest you make a local ajax request and on the server side connect to the cross domain server to get the data you want.

0


source


You can use flxhr from https://github.com/flensed/flXHR to trigger an ajax gateway 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;
        //
    }
}

      

0


source







All Articles