Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

php - jQuery AJAX error handling

I've searched the questions on here, but I don't have a good understanding of how to use the error handling in jQuery's AJAX (im a noob, so it just really doesn't make sense.)

Can anybody describe this to a beginner? I'm currently posting information to a PHP script via AJAX, but want to allow jQuery to recognize if the returned data from the script is an error or success.

Thanks! Dave

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error return from the ajax call is returning the results from a page load that was not successful. It may be that your php page returns a valid page, but with results that are not what you want. This is handled withing the success return. Hopefully the following code snippit will help illustrate...

$.ajax({
    type: "POST",
    url: "login.php",
    data: "action=login&user=" + user + "&pass=" + pass,
    success: function(xhr){
        if ((xhr == "Invalid Login") 
                || (xhr == "Invalid charaters in username.") 
                || (xhr == "Missing username or password.")
                || (xhr == "Unknown Error")) {
            $("#loginMessageContent").html(xhr);
        }
        else {
            simplemodalClose (dialog);
        }
   }, 
   error: function(xhr) {
       alert ("Oopsie: " + xhr.statusText);
   }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...