且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法使用 JS 和 AngularJS 获取 HTTP 状态代码名称?

更新时间:2023-08-31 10:05:52

如果我没记错的话,你仍然可以使用 jQuery ajax 进行调用,并使用 $.ajaxSetup 设置你的响应,如下所示:

If I'm not wrong, you can still use jQuery ajax for your call, and setup your response with $.ajaxSetup like this:

$.ajaxSetup({
    type: "GET",
    dataType: "jsonp",
    error: function(xhr, exception){
        if( xhr.status === 0)
            alert('Error : ' + xhr.status + 'You are not connected.');
        else if( xhr.status == "201")
            alert('Error : ' + xhr.status + '\nServer error.');
        else if( xhr.status == "404")
            alert('Error : ' + xhr.status + '\nPage note found');
        else if( xhr.status == "500")
             alert('Internal Server Error [500].');
        else if (exception === 'parsererror') 
            alert('Error : ' + xhr.status + '\nImpossible to parse result.');
        else if (exception === 'timeout')
            alert('Error : ' + xhr.status + '\nRequest timeout.');
        else
            alert('Error .\n' + xhr.responseText);
    }
});