Google Chrome doesn't allow AJAX requests over the file://
protocol by default.
You'll need to start Chrome with the --allow-file-access
switch.
Here are instructions for supplying switches on each OS.
I'm not sure how to enable the file protocol for IE.
Possibly the easiest thing to do is to setup a local web server and run your app along with the xml files in it, then you don't have to worry about this issue in either browser.
edit
Late edit, I know. I wanted to throw this out there in case others have an issue.
When requesting an xml document with jQuery, you should always supply a dataType in the settings object. Internally, jQuery does it's best to guess at what is returned and I've had it consider a perfectly good xml document to be HTML/XHTML.
To do this, add dataType: "xml". For example:
$(document).ready(function() {
$('.buttons').slideToggle('medium');
$.ajax({
url: "dictionary.xml",
success: function( xml ) {
$(xml).find("word").each(function(){
$("ul").append("<li>" + $(this).text() + "</li>");
});
},
dataType: "xml"
});
}
Supported data types are available on the documentation page for jQuery.ajax().
In addition, I've encountered the Access-Control-Allow-Origin error above when trying to fetch an RSS feed from a remote server. The only way I've determined to get around this is to proxy request the RSS feed from the server-side code and deliver it via some proxy script. Here's a simple example in PHP:
<?php
if(isset($_GET['q']) && isAjax())
{
$q = strip_tags($_GET['q']);
header("Status: 200");
header("Content-type: text/xml");
echo file_get_contents('http://'.$q);
exit();
}
function isAjax() {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}
?>
CAVEAT I highly recommend modifying the above script to allow only those locations you plan to serve to your client code by validating q
or by further restricting the isAjax
function. I'm not a PHP developer and I don't pretend to know best practices for PHP security. If anyone has suggestions to improve the PHP snippet, I'd happily apply them.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…