开发者

Why does this Ajax work in IE 7 and 8 but not FF or Chrome?

开发者 https://www.devze.com 2023-02-10 02:28 出处:网络
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

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消