jQuery XML Parse (with Ajax) Does Not Work in Chrome (but works in IE, FF, Opera, Safari)
I am using jQuery.ajax to parse some xml from a file. Everything works fine in IE (6,7,8), Firefox, Opera and Safari, but fails with Google Chrome. Here is the code:
/* ... */
this.loadXml = function()
{
$.ajax(
{
type: "G开发者_如何学运维ET",
url: "some_file.xml",
dataType: ($.browser.msie) ? "text" : "xml",
success: function(xml)
{
if($.browser.msie)
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(xml);
xml = xmlDoc;
}
/* parsing starts here */
/* for example: in the document I have a div tag with id "some text" and the xml file contains: <root><tag>test</tag></root>*/
$("#some_id").text($(xml).find("root > tag").text());
/* parsing ends here */
}
});
}
Chrome is not going to let you access a local file like that. It's a (somewhat contentious) security thing. You can start chrome from the command line with a flag to force it to allow access however:
google-chrome --allow-file-access-from-files
(My thanks for this again to @Nick Craver, man of infinite knowledge.)
edit — here's the question I asked: Accessing relative URL's via "ajax" from "file://" content
just replace the short url with the full url...
from
url: "some_file.xml",
to
url: "http://www.domain.com/some_file.xml",
精彩评论