jQuery $.get or $.post to catch page load error (eg. 404)

后端 未结 5 1306
予麋鹿
予麋鹿 2020-12-09 04:06

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)          


        
5条回答
  •  时光取名叫无心
    2020-12-09 05:02

    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

提交回复
热议问题