jQuery AJAX error handling

前端 未结 4 1762
半阙折子戏
半阙折子戏 2020-12-16 05:56

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 s

4条回答
  •  半阙折子戏
    2020-12-16 06:10

    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);
       }
    });
    

提交回复
热议问题