4

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

3
  • so you want some xml from corssdomain ajax. jsonp is the dataType for it. Commented Jan 28, 2013 at 10:57
  • but if i put jsonp how can i access i need xml Commented Jan 28, 2013 at 11:04
  • As I see, there is no "Access-Control-Allow-Origin:*" header. If you want to get xml by crossdomain ajax, it have to be. Commented Jan 28, 2013 at 11:06

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

8 Comments

@MuraliPrasanth: Yes, but as I say, there's a patch or plug-in around somewhere that updates jQuery to handle doing that for you...
thnx crowder but let me check if there is any plugin doing that work
jquery doesn't support XDomainRequest??
@MuraliPrasanth: That's correct, jQuery doesn't support the Microsoft-specific XDomainRequest object. It supports cross-domain requests (to the extent it has to explicitly support them), but not via the XDomainRequest object. This is intentional (in my view, it's misguided -- rather like XDomainRequest itself -- but intentional). Microsoft have seen the error of their ways and added CORS support to XMLHttpRequest like everyone else did in IE10 and later, but for IE8 and IE9 there's this gap. Hence the plug-in or patch.
can u give me the URL for a patch or plugin
|
0

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.

2 Comments

@T.J.Crowder sorry, I wasn't clear in the answer, I'll amend.
Not seeing why this is downvoted. I mean, yes, CORS requests are still Ajax request so there's a clarity thing, but I wouldn't downvote on that basis.
0

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.

1 Comment

"...expect json..." Assuming you meant "except", you can't make cross-domain calls for JSON, either. You can do JSONP, which is a different but similar-looking thing.
0

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;
        //
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.