angularjsで$http
サービスでリクエストを問い合わせた際、HTTPステータスコードが200系以外だと
返却されるPromiseにてsuccessではなくerrorが呼び出される。
検証コード
status.php
<?php http_response_code((int)$_GET['code']);
index.html
<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta charset="utf-8"> <meta name="description" content="sample html"> <meta name="author" content="packpak"> <meta name="viewport" content="width=device-width, intial-scale=1"> <link rel="shortcut icon" href=""> <script src="angular.min.js"></script> <script src="index.js"></script> </head> <body> <div ng-app="app"> <div ng-controller="mainCtrl as main"> <button ng-click="main.send()">get</button> </div> </div> </body> </html>
index.js
"use strict"; let app = angular.module('app', []); class MainController { constructor($scope, $http) { this.host = ''; this.$scope = $scope; this.$http = $http; } send() { for (let code of [200, 201, 204, 400, 401, 403, 404, 409, 500, 501]) { const url = "http://localhost/status.php?code=" + code; this.$http.get(url) .then( function(response) { console.log('status code:' + response.status) }, function(response) { console.error('status code:' + response.status) } ); } } }; app.controller('mainCtrl', ['$scope', '$http', MainController]);