AngularJs http获取响应
这是我的HTTP get请求AngularJs http获取响应,angularjs,error-handling,get,Angularjs,Error Handling,Get,这是我的HTTP get请求 $http({ method: 'GET', url: baseURL + '/couponManager/createCoupon?token=' + token + query1 + query2 + query3 + query4 + query5, }).then(function successCallback(response) {
$http({
method: 'GET',
url: baseURL + '/couponManager/createCoupon?token=' + token + query1 + query2 + query3 + query4 + query5,
}).then(function successCallback(response) {
if (response.status = 200)
{
$scope.showSuccessAlert = true;
$scope.successTextAlert = "Coupon successfully created!";
}
else if (response.status = 304)
{
$scope.errorAlert = true;
$scope.errorTextAlert = "Something Went Wrong, Please try again!";
}
}, function errorCallback(response) {
$scope.errorAlert = true;
$scope.errorTextAlert = "Can't reach server, please try later!";
});
这就是我得到的回应
{"success":"no","data":{},"error_code":"DW3","error_description":"Server Error"}
但是我收到的状态仍然是200,我如何从这个响应中检索“错误描述”和“状态”?如果(response.status==200)&
改变条件,否则如果(response.status==304)
你能显示后端代码吗?这取决于您从后端发送状态
和响应
的内容。1)使用参数
配置,而不是串联查询参数。2) 对于不成功的请求,后端应该返回实际的HTTP错误响应状态代码。3) 与==
或==
进行比较,而不是=
;这是分配4)您想要的数据实际上在response.data.success
,response.data.data
,response.data.error\u code
,等等中,您可以执行console.log(response)
并显示它的样子吗?@Phil非常感谢!我需要检查response.data.success。这就是我错过的我已经改变了谢谢!问题是即使在“成功”时也会得到200的回报:“否”。我如何才能为“成功”设定条件?这不是状态和响应不匹配的原因。它的后端决定要发送的状态和响应。但您指出了一个很好的错误。那么您的意思是“status=200”即使在“success is no”时也由后端发送?因为后端是由其他人编写的,status=200
表示您正在成功地从后端获得响应。它没有说成功与否是的,我必须用response.data.error\u代码来处理错误代码,谢谢