简体   繁体   English

如何使用 JavaScript 或 jQuery 从另一台服务器解析 XML?

[英]How to parse XML from another server using JavaScript or jQuery?

I am new to Javascript. I need to parse XML using javascript or jQuery which is another server.我是 Javascript 的新手。我需要使用另一台服务器 javascript 或 jQuery 来解析 XML。 I tried by using the below code.我尝试使用以下代码。 But when I executed it, it was not going to success method.但是当我执行它时,它不会成功。

I was able to parse the XML which is in the same folder.我能够解析同一文件夹中的 XML。 Is it possible in javascript to access content from another server.是否可以在 javascript 访问另一台服务器的内容。 I read the Same Origin Policy.我阅读了同源政策。

I was able to get the success message, but cant get the xml data我能够获得成功消息,但无法获得 xml 数据

$.ajax({

type: 'GET',
url: 'http://10.47.5.69/myxml.xml',  
dataType: "xml",

success: function(data){
    alert('success');
    $(data).find("Node").each(function() {

         alert($(this).find("element").text());
        });
    },
    error: err  
});

function err(xhr, reason, ex)
{
    alert('xhr.status: ' + xhr.status);
    alert('ex "'+ex);
}

You cannot load something from another server due to cross-domain security checks.由于跨域安全检查,您无法从另一台服务器加载内容。

However for javascript, there is a workaround: the JSONP technique: http://en.wikipedia.org/wiki/JSONP但是对于 javascript,有一个解决方法:JSONP 技术: http://en.wikipedia.org/wiki/JSONP

It is used for JSON data but it can just as well be used for any string data.它用于 JSON 数据,但也可以用于任何字符串数据。 But it will only work if you have some degree of control (ie can install a script) on that server.但只有当您对该服务器有一定程度的控制(即可以安装脚本)时,它才会起作用。

Another alternative is to proxy that URL on your own server.另一种选择是在您自己的服务器上代理 URL。 That might be easier.那可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM