下面是我的场景:
我的控制器就像:
var userData = $http(
{
method: "post",
url: "http://some-domain.com/t-app/mobile-data/login.php",
data : $scope.loginForm, //forms user object
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
userData.success(function (userdataobject)
{
$rootScope.status_id = userdataobject["status_id"];
});我知道这只有在互联网连接可用的情况下才能起作用。我的问题是,在这个场景中,我如何知道像"404“这样的错误状态,或者如果internet连接不可用?
发布于 2015-12-30 11:34:49
有status字段(来自文档):
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(
function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
},
function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
if (response.status === 404)
{
//your code here
}
});在你的情况下是:
$http(
{
method: "post",
url: "http://some-domain.com/t-app/mobile-data/login.php",
data : $scope.loginForm, //forms user object
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.then(
function(response) {
$rootScope.status_id = userdataobject.data["status_id"];
},
function(response) {
if (response.status === 404) {
//your code here
}
}
);发布于 2015-12-30 11:40:57
关于你的例子:
var userData = $http({
method: "post",
url: "http://some-domain.com/t-app/mobile-data/login.php",
data : $scope.loginForm, //forms user object
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
userData.success(function (userdataobject) {
$rootScope.status_id = userdataobject["status_id"];
}).catch(function(errorResponse, status) {
console.error(errorResponse); //for debugging
if (errorResponse.status == 404) {
//Handle 404 error
}
//or if (status == 404) {}
});errorResponse将包含以下字段:
发布于 2015-12-30 11:42:45
您的代码可能如下所示:
$http(
{
method: "post",
url: "http://some-domain.com/t-app/mobile-data/login.php",
data : $scope.loginForm, //forms user object
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response) {
$rootScope.status_id = response["status_id"];
}, function errorCallback(response) {
console.error(response.status);
console.error(response.statusText);
});这样,您就可以控制成功和错误。
https://stackoverflow.com/questions/34528669
复制相似问题