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

ame*_*ire 5 javascript http angularjs dhc

我有一个AngularJS,JS,JQ,HTML5,CSS3网络应用程序.它可以将不同的HTTP方法发送到我们项目的REST API并对其进行操作.它与Restlet(以前的Dev HTTP Client)的DHC具有类似的行为.每个请求都返回一个状态代码,如200,201,404,500等,然后显示给用户.

现在我想要的不仅是显示响应代码,还要显示它的描述如下:

404 Not Found,201 Created等等.

我得到Angular的回复,如下所示:

$http(config).success(function (data, status, headers, config) {//some logic}
Run Code Online (Sandbox Code Playgroud)

有没有人知道使用AngularJS是否可行?

ame*_*ire 8

好吧,我最终得到了以下解决方案:

我创建了一个常量变量并列出了所有HTTP代码及其描述(您可以将它们复制到您自己的程序中):

constants.js:

var HTTP_STATUS_CODES = {
        'CODE_200' : 'OK',
        'CODE_201' : 'Created',
        'CODE_202' : 'Accepted',
        'CODE_203' : 'Non-Authoritative Information',
        'CODE_204' : 'No Content',
        'CODE_205' : 'Reset Content',
        'CODE_206' : 'Partial Content',
        'CODE_300' : 'Multiple Choices',
        'CODE_301' : 'Moved Permanently',
        'CODE_302' : 'Found',
        'CODE_303' : 'See Other',
        'CODE_304' : 'Not Modified',
        'CODE_305' : 'Use Proxy',
        'CODE_307' : 'Temporary Redirect',
        'CODE_400' : 'Bad Request',
        'CODE_401' : 'Unauthorized',
        'CODE_402' : 'Payment Required',
        'CODE_403' : 'Forbidden',
        'CODE_404' : 'Not Found',
        'CODE_405' : 'Method Not Allowed',
        'CODE_406' : 'Not Acceptable',
        'CODE_407' : 'Proxy Authentication Required',
        'CODE_408' : 'Request Timeout',
        'CODE_409' : 'Conflict',
        'CODE_410' : 'Gone',
        'CODE_411' : 'Length Required',
        'CODE_412' : 'Precondition Failed',
        'CODE_413' : 'Request Entity Too Large',
        'CODE_414' : 'Request-URI Too Long',
        'CODE_415' : 'Unsupported Media Type',
        'CODE_416' : 'Requested Range Not Satisfiable',
        'CODE_417' : 'Expectation Failed',
        'CODE_500' : 'Internal Server Error',
        'CODE_501' : 'Not Implemented',
        'CODE_502' : 'Bad Gateway',
        'CODE_503' : 'Service Unavailable',
        'CODE_504' : 'Gateway Timeout',
        'CODE_505' : 'HTTP Version Not Supported'
    };
Run Code Online (Sandbox Code Playgroud)

然后在我的AngularJS控制器,当我收到一个状态由$http我只是调用这个函数,它设置的状态码消息的一个模型中的价值:

setResponseCodeDescr = function(responseCode) {

var responseCodeConstant = 'CODE_'.concat(responseCode);

if (HTTP_STATUS_CODES[responseCodeConstant]){
    $rootScope.response.description = HTTP_STATUS_CODES[responseCodeConstant];
} else {
    $rootScope.response.description = "";
}

}
Run Code Online (Sandbox Code Playgroud)

就这样 :)

  • 对于"全部"的某些价值. (4认同)

Dim*_*tri 0

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

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)