jQuery的AJAX错误处理 [英] jQuery AJAX error handling

查看:136
本文介绍了jQuery的AJAX错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查放在这里的问题,但我没有对如何使用错误处理jQuery的AJAX很好地理解(我是个菜鸟,所以它只是真的没有意义。)

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.)

有人能说说这是初学者?我目前发布的信息通过AJAX PHP脚本,但想要让jQuery的认识,如果从脚本返回的数据是错误或成功。

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.

谢谢! 戴夫

推荐答案

从Ajax调用返回的错误是返回从页面加载,这不是成功的结果。这可能是因为你的PHP页面会返回一个有效的网页,但结果是不是你想要的。这是withing成功返回处理。幸运地是,code的这段将有助于说明...

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

这篇关于jQuery的AJAX错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆