问题与jquery ajax跨域xml响应

时间:2022-08-23 10:07:17

Here is my code to access xml from a website

这是我从网站访问xml的代码

$(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

这是JsFiddle

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

我知道这是一个跨域请求但是,我该如何解决?

Thanks

4 个解决方案

#1


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.)

使用XML,真正的跨域请求唯一真正的选择是,如果该服务器支持CORS,允许您的源,并且您的浏览器支持它。 (如果他们有一个JSONP选项,那会更容易。但遗憾的是,快速查看他们的API页面表明他们只支持XML和JSON,而不是JSONP。但是要自己看看,不要相信我的话,我没有详细阅读。在我看来,如果他们支持JSON但不支持JSONP,那有点奇怪。)

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

我有时听过但是已经讨论过的另一种选择是使用YQL作为跨域代理。

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.

当然,您也可以运行自己的服务器,向它发出请求,并让它查询rxnav.nlm.nih.gov提要并将其返回给您。然后SOP没有进入它。

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.

旁注:要在IE8或IE9中使用带有jQuery的CORS,您需要一个处理使用特殊XDomainRequest对象的插件(IE8和IE9的XMLHttpRequest对象不执行CORS)。 IE10终于解决了这个问题。

#2


0  

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

您只能在跨域请求中使用CORS或JSONP。用于获取XML的AJAX请求将无法跨域工作

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.

解决方法是使用PHP / ASP.Net /语言在本地服务器上创建服务器端代理,然后通过AJAX调用它。

#3


0  

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

您无法通过ajax(期望json)发出跨域请求。

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

我建议您发出本地ajax请求,并在服务器端连接到跨域服务器以获取您需要的数据。

#4


0  

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

您可以使用https://github.com/flensed/flXHR中的flxhr来运行跨域ajax调用

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

#1


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.)

使用XML,真正的跨域请求唯一真正的选择是,如果该服务器支持CORS,允许您的源,并且您的浏览器支持它。 (如果他们有一个JSONP选项,那会更容易。但遗憾的是,快速查看他们的API页面表明他们只支持XML和JSON,而不是JSONP。但是要自己看看,不要相信我的话,我没有详细阅读。在我看来,如果他们支持JSON但不支持JSONP,那有点奇怪。)

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

我有时听过但是已经讨论过的另一种选择是使用YQL作为跨域代理。

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.

当然,您也可以运行自己的服务器,向它发出请求,并让它查询rxnav.nlm.nih.gov提要并将其返回给您。然后SOP没有进入它。

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.

旁注:要在IE8或IE9中使用带有jQuery的CORS,您需要一个处理使用特殊XDomainRequest对象的插件(IE8和IE9的XMLHttpRequest对象不执行CORS)。 IE10终于解决了这个问题。

#2


0  

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

您只能在跨域请求中使用CORS或JSONP。用于获取XML的AJAX请求将无法跨域工作

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.

解决方法是使用PHP / ASP.Net /语言在本地服务器上创建服务器端代理,然后通过AJAX调用它。

#3


0  

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

您无法通过ajax(期望json)发出跨域请求。

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

我建议您发出本地ajax请求,并在服务器端连接到跨域服务器以获取您需要的数据。

#4


0  

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

您可以使用https://github.com/flensed/flXHR中的flxhr来运行跨域ajax调用

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