5

有没有办法将数字 http 状态代码(即 404,403,500,...)转换为它们的显示名称(未找到,禁止,内部服务器错误,...)?

如果它有任何帮助,我正在使用 AngularJS 和 jQuery。

4

5 回答 5

5

您可以构建自己的对象(或下载对象)并在那里查找它们:

var codes = {
    "100": "Continue",
    "101": "Switching Protocols",
    "102": "Processing",
    "200": "OK",
    ...
};

var code = 200;

console.log(codes[code]); // "Ok"
于 2012-12-03T15:40:07.117 回答
3

您可以使用来自 npm的 http-status-codes 。

    var HttpStatus = require('http-status-codes');
    HttpStatus.getStatusText(200) // ==> "OK"
于 2020-03-11T15:46:53.830 回答
2

只是为了帮助大家复制/粘贴

/* Helpers */
var friendlyHttpStatus = {
    '200': 'OK',
    '201': 'Created',
    '202': 'Accepted',
    '203': 'Non-Authoritative Information',
    '204': 'No Content',
    '205': 'Reset Content',
    '206': 'Partial Content',
    '300': 'Multiple Choices',
    '301': 'Moved Permanently',
    '302': 'Found',
    '303': 'See Other',
    '304': 'Not Modified',
    '305': 'Use Proxy',
    '306': 'Unused',
    '307': 'Temporary Redirect',
    '400': 'Bad Request',
    '401': 'Unauthorized',
    '402': 'Payment Required',
    '403': 'Forbidden',
    '404': 'Not Found',
    '405': 'Method Not Allowed',
    '406': 'Not Acceptable',
    '407': 'Proxy Authentication Required',
    '408': 'Request Timeout',
    '409': 'Conflict',
    '410': 'Gone',
    '411': 'Length Required',
    '412': 'Precondition Required',
    '413': 'Request Entry Too Large',
    '414': 'Request-URI Too Long',
    '415': 'Unsupported Media Type',
    '416': 'Requested Range Not Satisfiable',
    '417': 'Expectation Failed',
    '418': 'I\'m a teapot',
    '429': 'Too Many Requests',
    '500': 'Internal Server Error',
    '501': 'Not Implemented',
    '502': 'Bad Gateway',
    '503': 'Service Unavailable',
    '504': 'Gateway Timeout',
    '505': 'HTTP Version Not Supported',
};

/* Usage */
var code = 200;

console.log(friendlyHttpStatus[code]); // "Ok"
于 2017-01-10T12:13:05.957 回答
1

我不这么认为,但您可以使用这个有用的网站:http ://httpstat.us/ 。

于 2012-12-03T15:36:29.293 回答
1

我蹩脚的解决方案:

var friendlyHttpStatus = function(status){

    if(status == '200') return 'OK'
    if(status == '201') return 'Created'
    if(status == '202') return 'Accepted'
    if(status == '203') return 'Non-Authoritative Information'
    if(status == '204') return 'No Content'
    if(status == '205') return 'Reset Content'
    if(status == '206') return 'Partial Content'
    if(status == '300') return 'Multiple Choices'
    if(status == '301') return 'Moved Permanently'
    if(status == '302') return 'Found'
    if(status == '303') return 'See Other'
    if(status == '304') return 'Not Modified'
    if(status == '305') return 'Use Proxy'
    if(status == '306') return 'Unused'
    if(status == '307') return 'Temporary Redirect'
    if(status == '400') return 'Bad Request'
    if(status == '401') return 'Unauthorized'
    if(status == '402') return 'Payment Required'
    if(status == '403') return 'Forbidden'
    if(status == '404') return 'Not Found'
    if(status == '405') return 'Method Not Allowed'
    if(status == '406') return 'Not Acceptable'
    if(status == '407') return 'Proxy Authentication Required'
    if(status == '408') return 'Request Timeout'
    if(status == '409') return 'Conflict'
    if(status == '410') return 'Gone'
    if(status == '411') return 'Length Required'
    if(status == '412') return 'Precondition Required'
    if(status == '413') return 'Request Entry Too Large'
    if(status == '414') return 'Request-URI Too Long'
    if(status == '415') return 'Unsupported Media Type'
    if(status == '416') return 'Requested Range Not Satisfiable'
    if(status == '417') return 'Expectation Failed'
    if(status == '418') return 'I\'m a teapot'
    if(status == '500') return 'Internal Server Error'
    if(status == '501') return 'Not Implemented'
    if(status == '502') return 'Bad Gateway'
    if(status == '503') return 'Service Unavailable'
    if(status == '504') return 'Gateway Timeout'
    if(status == '505') return 'HTTP Version Not Supported'
}
于 2014-03-18T18:39:03.963 回答