如何捕捉阿贾克斯查询后错误? [英] how to catch ajax query post error?

查看:196
本文介绍了如何捕捉阿贾克斯查询后错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获错误,如果AJAX请求失败显示相应的信息。 我的code是像下面,但我不能设法赶上失败Ajax请求。

I would like to catch the error and show the appropriate message if the ajax request fails. My code is like the following, but I could not manage to catch the failure ajax request.

function getAjaxData(id)    
{
     $.post("status.ajax.php", {deviceId : id}, function(data){

        var tab1;

        if (data.length>0) {               
            tab1 = data;
        } 
        else {
            tab1 = "Error in Ajax";
        }       

        return tab1;
    });
}

我发现,永远不会执行在阿贾克斯错误时,Ajax请求失败。 如何处理Ajax的错误,如果失败显示相应的消息?非常感谢。

I found out that, "Error in Ajax" is never executed when the Ajax request failed. How to handle the ajax error and show the appropriate message if it fails? Thanks very much.

推荐答案

您可以使用阿贾克斯

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston",
  success: function(msg){
        alert( "Data Saved: " + msg );
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
     alert("some error");
  }
});

这篇关于如何捕捉阿贾克斯查询后错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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