简体   繁体   English

浏览器使用JQuery解析XML问题

[英]Browser Problems Parsing XML With JQuery

I need to access XML files with JQuery. 我需要使用JQuery访问XML文件。 I tried these two ways but I can't seem to contain them on one browser. 我尝试了这两种方式,但似乎无法将它们包含在一个浏览器中。

Here: 这里:

In Chrome, this works: 在Chrome浏览器中,这有效:

var xml = "<music><album>Beethoven</album></music>";
var result = $(xml).find("album").text();
alert(result);

Now I try to use it in conjunction with this code snippet, which works fine in IE: 现在,我尝试将其与此代码段结合使用,在IE中可以正常工作:

jQuery.get('fruits.xml', function(data) {
    alert(data);
});

The problem is, if I put both these codes together, one of them don't work on the other. 问题是,如果我将这两个代码放在一起,则其中一个不会在另一个上运行。 So in Chrome I'd be able to access "Beethoven" while not being able to access "fruits.xml" but gives me this error: 因此,在Chrome浏览器中,我可以访问“贝多芬”,而不能访问“ fruits.xml”,但会出现以下错误:

XMLHttpRequest cannot load file:///C:/Python32/fruits.xml. Origin null is not allowed by Access-Control-Allow-Origin.

In IE on the other hand, I could access the whole content of fruits.xml and save it into a variable but the code for which I need to access XML attributes which works in Chrome doesn't work in IE. 另一方面,在IE中,我可以访问fruit.xml的全部内容并将其保存到变量中,但是我需要访问在Chrome中工作的XML属性的代码在IE中不起作用。

As you can see, I want to get the contents of the xml using the 2nd snippet of code, while I will access the contents of the xml using the 1st snippet of code. 如您所见,我想使用第二个代码段来获取xml的内容,而我将使用第一个代码段来访问xml的内容。 Is there another way to access XML with Javascript? 还有另一种使用Javascript访问XML的方法吗? Can you help me with what's wrong with my codes? 您能帮我解决我的代码有什么问题吗?

Help? 救命?

The problem causing the error message is that you're sending an XHRRequest (The A in *A*JAX) to a file:// URL. 导致错误消息的原因是您正在将XHRRequest(* A * JAX中的A)发送到file:// URL。 For security reasons, this is being disabled by modern browsers. 出于安全原因,现代浏览器已禁用此功能。 Instead, set up a webserver und it to access your page, via http://localhost/.. instead of file://C:/... . 而是设置一个Web服务器并通过http://localhost/..而不是file://C:/...来访问您的页面。

For the second part, make sure that you test loading fruits.xml first. 对于第二部分,请确保首先测试加载fruits.xml Chances are there is an error in the XML stored in this file, or its structure is not what you expect. 可能是此文件中存储的XML中有错误,或者它的结构不是您期望的。 You can debug JavaScript in IE by pressing F12 , going to the Scripting tab, and clicking Start Debugger . 您可以通过按F12进入Scripting选项卡,然后单击Start Debugger来在IE中调试JavaScript。 That way, you'll get a better error description than "doesn't work". 这样,您将获得比“不起作用”更好的错误描述。

this may be a bug associated with chrome 这可能是与Chrome相关的错误

loading local files in chrome bug/security feature 在Chrome错误/安全功能中加载本地文件

Problems with jQuery getJSON using local files in Chrome 使用Chrome中的本地文件的jQuery getJSON问题

Accessing relative URL's via "ajax" from "file://" content 通过“ ajax”从“ file://”内容访问相对URL

as an advice dont wrap your xml in jquery and parse this way it may vary from browser to browser use instead .parseXML 建议不要将xml封装在jquery中并以这种方式进行解析,这可能因浏览器的不同而不同,而使用.parseXML

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

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