hh54188
hh54188

Reputation: 15656

jquery $.ajax() in safari and chrome doesn't work

I want use $.ajax to read some infomation from xml file,here is my js code :

        $.ajax({
            type: "get",
            url: "Database/App_all.xml",
            dataType: "xml",
            timeout: 2000,
            beforeSend: function () {

            },
            success: function (xml) {
                $(xml).find("app[id='id-1']").appendTo($("#contain"));
            },
            error: function () {
                alert("ajax failed!");
            }
        });

However, the code only work great in firefox and opera.

It doesn't work in chrome(7.0.517.24 ) and safari(5.0.1),failed without any alert,not even the alert("ajax failed").

Is there any bug in $.ajax in chrome and safari?so how to solve the problem?

thank you very much:)

Upvotes: 0

Views: 11248

Answers (5)

Deepali
Deepali

Reputation: 11

This is a problem for local files... You should try uploading them on a web server and check from there

Upvotes: 0

Oleg
Oleg

Reputation: 222017

I suppose you have problem with reading of the local file per ajax. Ajax can be used to read a file from the same web server, but there are some security restriction if you read it not per HTTP.

In firefox and opera you can read local files (with url like file:///C:/Program%20Files/My/Database/App_all.xml) per ajax without any problem.

In Internet Explorer you should use dataType: 'text' and then convert the text to XML (read more here).

To be able to read local files in Chrome you have to restart chrome with another parameters:

chrome.exe --allow-file-access-from-files

(Be sure that all other instances of chorme closed before starting Chrome.exe in the way).

Upvotes: 0

hh54188
hh54188

Reputation: 15656

First thank you gajendra.bang and Māris Kiseļovs give me your advices,I have konw what's wrong with my code,after I get a bad resault ,I trying to know what the $.ajax get from xml exactly,so I use firebug check the div#contain I found that:

 <div id="contain">
        <auther>cocept</auther>
 </div>

yes,I think the <auther></auther> must the problem,I don't even konw the $.ajax would get the tagname as well so I rewrite it :

  success: function (xml) {
        $("#contain").html($(xml).find("app[id='id-1']").find("auther").text());
}

then the div$contain is:

 <div id="contain">
        cocept
 </div>

so ,the chrome and safari could show again!

Upvotes: 1

Gajendra Bang
Gajendra Bang

Reputation: 3583

 $(xml).find("app[id='id-1']").appendTo($("#contain"));

what is xml basically returning, an element with "#" like "#mydiv" or class like ".mydiv"

I think you are trying to access an element and if you are not returning it with "#", try

 $("#"+xml).find("app[id='id-1']").appendTo($("#contain"));

Upvotes: -1

Māris Kiseļovs
Māris Kiseļovs

Reputation: 17305

You should use chrome's or safari's built-in developer tools (ctrl+shift+i) to track JS errors and track actual AJAX requests.

Is your code wrapped in document.ready? Is there any erros in javascript console? Also try to output something after success callback line.

Another cause for this could be incorrect mime-type for your XML file returned by server. It should be [Content-type: text/xml]. You can check that in chrome's or safari's built-in developer tools - just look for headers tab when xml resource is selected. If it 's actual problem, you may need to tweak web-server configuration (main config or .htaccess for apache) to return correct mime-type.

Upvotes: 4

Related Questions