Vitor Santos
Vitor Santos

Reputation: 314

How can I detect an error in AJAX?

I have a jQuery function, which is using AJAX to send the necessary information to run the respective script properly:

$("#changeUseridForm").submit(function(){
    $.ajax({
        type: "GET",
        url: "API/root/users/changeUsername.php",
        data: {
        newUsername: ("#newUserid", this).val(),
        password: ("#retypePass", this).val(),
        xml: 1,
        },
        dataType: 'xml',
        success: function(xml){
            if($(xml).find("success").length > 0){
                alert("Username changed successfully!");

                $("#changeUserid").hide();
                $("#BackToMainMenu").hide();
                $("#MainPage").show();
                $("#AddLinkButton").show();
                $("#ChangeUserOptions").show();
                $("#ChangeUserDataButton").show();
                $("#ShowPosts").show();
            }
            else if($(xml).find("error").length > 0){
                alert("You have to fill all the fields!");
            }
        }
    });

    return false;
});

I have several functions like this one, running perfectly; this one isn't. I verified all my variables and scripts. They're spelled correctly. It doesn't reach to the script referenced. I think the AJAX code might have a problem, but I can't detect which error is. I tried to search it on my browser's web inspector, but I can't figure it out since the page is reloading for some reason that I don't know why. (Because this function doesn't have window.location.reload() in it anywhere.)

Upvotes: 1

Views: 422

Answers (3)

Igor Parra
Igor Parra

Reputation: 10348

You can detect (debug) a lot of errors using firebug. In this case you can use the net tab and check persists to see what request is causing reloaded page.

enter image description here

Upvotes: 0

Pekka
Pekka

Reputation: 449385

Look in the docs on .ajax(), you can specify an error handler function that gets details about what went wrong. That would be the first step.

You can also use Firebug's or Chrome's "Net" tab to monitor the request, and see what was returned.

Upvotes: 1

Ben
Ben

Reputation: 2024

try to set up a proxy or use firefox plugin to catch the get request(you can also use wireshark)

then you can see if there's a request to this page API/root/users/changeUsername.php it is possible that this page API/root/users/changeUsername.php returns 302 redirect,

Check the http response of the GET Request and post it please

Upvotes: 1

Related Questions