jQuery $ .get或$ .post来捕捉页面加载错误(例如404) [英] jQuery $.get or $.post to catch page load error (eg. 404)

查看:118
本文介绍了jQuery $ .get或$ .post来捕捉页面加载错误(例如404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用$ .get或$ .post时,您如何捕获Server Error或404页面?



例如:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b});

如果加载/ myhandler时发生服务器错误,或者如果是找不到。



如果出现错误,您如何通知您?

解决方案

中使用错误 code $ $ ajax()

  $。ajax({ 
URL: /将myHandler,
数据:{值:1},
型: '后',
错误:功能(XMLHttpRequest参数url errorThrown){
警报( '状态:' + XMLHttpRequest.status + '状态文本:' + XMLHttpRequest.statusText);
},
成功:功能(数据){}
});



demo


How do you catch Server Error or 404 page not found, when you use $.get or $.post ?

For example:

$.post("/myhandler", { value: 1 }, function(data) {
  alert(data);
});

That will do absolutely nothing if there is a Server Error loading "/myhandler", or if it is not found.

How do you make it notify you if there is an error?

解决方案

use error handler on $.ajax()

$.ajax({
    url: "/myhandler", 
    data: {value: 1},
    type: 'post',
    error: function(XMLHttpRequest, textStatus, errorThrown){
        alert('status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
    },
    success: function(data){}
});

demo

这篇关于jQuery $ .get或$ .post来捕捉页面加载错误(例如404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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