开发者

What is wrong with my ajax code?

开发者 https://www.devze.com 2023-03-01 13:06 出处:网络
I\'m trying to read a xml file but the ajax code I have keeps going to error instead of success. What could be causing this?

I'm trying to read a xml file but the ajax code I have keeps going to error instead of success. What could be causing this?

$.ajax({
        type: "GET",
        url: "xml/gallery.xml",
        dataType: "xml",
        success: function(xml) {
            alert("A开发者_运维知识库sd");

        },
        error: function() {
            alert("ad");
        }
    });

xml

<?xml version="1.0" encoding="utf-8" ?>
<gallery>
    <image src="images/field.jpg"/>
</gallery>


Try changing your error handler to

error: function(jqXHR, textStatus, errorThrown){alert(textStatus + ': ' + errorThrown);}

This should allow you to see more information about the error.


AJAX requests can't access the local file system, so requests like this will fail.

You will need to have the page up on a Web Server.

You can use XAMPP or similar for a basic Web Server to test on you local machine.


What is the location of the page your Ajax function on, and what is the absolute path to the XML file you are trying to load?

You are referencing your XML using a relative path (xml/gallery.xml), so if the XML is not in a subdirectory xml RELATIVE to the page you are using the Ajax function on, this will fail. That may be what's causing your issue.


Are you running the website using a LAMP Stack (or something like XAMPP?). If you are trying to run the above code directly from an html file on your desktop it's not going to work.

http://api.jquery.com/jQuery.ajax/

I'd strongly reccomend using something like XAMPP or even better run a copy of linux (either on a computer or as a VM) - that matches the environment you are planning to deploy in.

0

精彩评论

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