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

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

问题描述

当您使用 $.get 或 $.post 时,您如何捕获服务器错误或未找到 404 页面?

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

例如:

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

如果加载/myhandler"时出现服务器错误,或者没有找到,那绝对不会做任何事情.

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

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

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

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