为什么这个Ajax在IE 7和8中工作但不在FF或Chrome中工作?

时间:2022-05-17 17:33:14

Say I'm using the following Ajax call:

假设我正在使用以下Ajax调用:

$(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:

这在IE 7和8中工作正常,但在FF或Chrome中不起作用。我得到一个空的XML文件,并在这些浏览器中出现以下错误:

XML Parsing Error: no element found Location: moz-nullprincipal:{77f5fd10-d793-4d35-9a4b-b8280b704fba} Line Number 1, Column 1:

XML解析错误:找不到元素位置:moz-nullprincipal:{77f5fd10-d793-4d35-9a4b-b8280b704fba}第1行,第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.

当我用Google搜索错误时,我认为这是由于Ajax跨域问题。但如果是这样的话,不会在所有浏览器中禁用它吗?任何帮助都表示赞赏 - 我对这整个Ajax事物都是新手。

Thanks!

谢谢!

2 个解决方案

#1


0  

You are making an AJAX call from a client to an outside domain (unless you're writing code for www.w3schools.com.

您正在从客户端到外部域进行AJAX调用(除非您正在为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.

这可能是SOP(同源政策)问题。您可以尝试使用JSONP(如果他们的服务器已设置),或者您可以将该调用移动到您的服务器(PHP,C#等等)并让您的服务器代表客户端进行调用。

I suggest googling "SOP" and "JSONP".

我建议谷歌搜索“SOP”和“JSONP”。

#2


1  

    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.

我想知道你是否收到错误,因为实际上有错误或者你误解了lambda表达式。我引用的行总是会弹出一条错误信息。

    asynch: true

You also misspelled async. You really need to be more careful with what you type.

你也错误拼写了异步。你真的需要更加小心你输入的内容。

#1


0  

You are making an AJAX call from a client to an outside domain (unless you're writing code for www.w3schools.com.

您正在从客户端到外部域进行AJAX调用(除非您正在为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.

这可能是SOP(同源政策)问题。您可以尝试使用JSONP(如果他们的服务器已设置),或者您可以将该调用移动到您的服务器(PHP,C#等等)并让您的服务器代表客户端进行调用。

I suggest googling "SOP" and "JSONP".

我建议谷歌搜索“SOP”和“JSONP”。

#2


1  

    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.

我想知道你是否收到错误,因为实际上有错误或者你误解了lambda表达式。我引用的行总是会弹出一条错误信息。

    asynch: true

You also misspelled async. You really need to be more careful with what you type.

你也错误拼写了异步。你真的需要更加小心你输入的内容。