5

AngularJS、JS、JQ、HTML5、CSS3 Web アプリがあります。プロジェクトの REST API にさまざまな HTTP メソッドを送信して操作することを想定しています。Restlet (以前の Dev HTTP Client) による DHCと同様の動作をします。すべてのリクエストは、200、201、404、500 などのステータス コードを返し、ユーザーに表示されます。

今私が望むのは、応答コードだけでなく、次のような説明も表示することです:

404 Not Found201 Createdなど_

Angular から次のような応答が返ってきました。

$http(config).success(function (data, status, headers, config) {//some logic}

AngularJS を使用してこれが可能かどうかは誰にもわかりませんか?

4

5 に答える 5

12

さて、私は次の解決策になりました:

定数変数を作成し、すべての HTTP コードとその説明をリストしました (それらを自分のプログラムにコピーできます)。

定数.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'
    };

次に、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 = "";
}

}

それで全部です :)

于 2014-04-10T13:44:10.343 に答える
0

技術的にコードを取得するのは簡単に思えますが、「応答の組み込みプロパティを添付するだけで説明が得られます」。

実際にはドキュメントで説明されています。応答とそのプロパティを取得したら、それを使って何でもできます。もう 1 つの方法は CONSTANT を作成することですが、通常はそれほど多くは必要ないと思います。

$http({
    method: 'GET',
    url: '/someUrl'
}).then(function successCallback(response) {
    var status = response.status;
    console.log(status); // gives the status 200/401
    var statusText = response. statusText;
    console.log(status); // HTTP status text of the response
}, function errorCallback(response) {

});
于 2015-10-23T00:56:48.560 に答える
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);
    }
});
于 2014-04-10T12:15:51.707 に答える