$ httpサービスは、HTTP要求を生成して約束を返すための単一の引数(構成オブジェクト)を受け取る関数です。
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response); // add console log of response
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response); // add console log of error response
});
それともインターセプタは、すべてのHTTPの「監視」するために使用することができます。
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
// optional method
'request': function(config) {
// do something on success
return config;
},
// optional method
'requestError': function(rejection) {
// do something on error
if (response.status === 500) {
//DO WHAT YOU WANT
}
return $q.reject(rejection);
},
// optional method
'response': function(response) {
// do something on success
return response;
},
// optional method
'responseError': function(rejection) {
// do something on error
if (response.status === 500) {
//DO WHAT YOU WANT
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
を、これはエラーに表示されるために適切なウィンドウを引き起こした、ありがとうございました。 Webブラウザのコンソールウィンドウにサーバーエラーが表示されないようにする方法はありますか? – cAMPy
この例では、responseErrorの 'response.status === 500'を更新しました。最初の例では、console.loggingエラーです。または、あなたは何のコンソールエラーについて話していますか? :) –
私は適切にユーザーに適切なメッセージを500エラーで表示できることを意味します。しかし、誰かがコンソールを起動した場合、「NetworkError:500 Internal Server Error - 」とサーバーのURLが表示されます。これは重要ではありませんが、このメッセージをコンソールに表示しないようにすることができれば、私はそうしたいと思います。 – cAMPy