Say I'm using the following Ajax call:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://www.w3schools.com/xml/cd_catalog.xml", //test xml
dataType: "xml",
success: xmlParser,
error: alert("We can't find your XML!"),
asynch: true
});
});
function xmlParser(xml) {
$(xml).find("CD:lt(3)").each(function () {
$("#offers").append('<h1>' + $(this).find("ARTIST").text() + '</h1><p>' + $(this).find("YEAR").text() + '</p>');
});
This works fine in IE 7 and 8, but doesn't work in FF or Chrome. I get an empty XML file and the following error in those browsers: 开发者_如何学C
XML Parsing Error: no element found Location: moz-nullprincipal:{77f5fd10-d793-4d35-9a4b-b8280b704fba} Line Number 1, Column 1:
When I googled the error, I thought that it was due to the Ajax cross-domain issue. But if that were the case, wouldn't it be disabled in all browsers? Any help is appreciated - I'm kinda new to this whole Ajax thing.
Thanks!
error: alert("We can't find your XML!"),
I wonder if you get the error because there actually is an error or because you misunderstand lambda expressions. The line I quoted will always popup an error message.
asynch: true
You also misspelled async
. You really need to be more careful with what you type.
You are making an AJAX call from a client to an outside domain (unless you're writing code for www.w3schools.com.
This could be an SOP (Same Origin Policy) issue. You could try using JSONP (if their server is set up for that), or you can move that call to your server (PHP, C#, etc..) and have your server make the call on behalf of the client.
I suggest googling "SOP" and "JSONP".
精彩评论