【问题标题】:AngularJs http get responseAngularJs http 得到响应
【发布时间】:2017-04-18 05:35:18
【问题描述】:

这是我的 HTTP 获取请求

 $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,如何从这个响应中检索“error_description”和“status”?

【问题讨论】:

  • 你能显示backend 代码吗?这取决于您从后端发回 statusresponse 的内容。
  • 1) 使用params 配置而不是连接查询参数。 2) 您的后端应该为不成功的请求返回一个实际的 HTTP 错误响应状态代码。 3) 与===== 进行比较,而不是=;那是为了分配
  • 4) 你要的数据其实在response.data.successresponse.data.dataresponse.data.error_code等中
  • 你能做到console.log(response) 并展示它的样子吗?
  • @Phil 非常感谢!我需要检查 response.data.success。这就是我所缺少的

标签: angularjs error-handling get


【解决方案1】:

更改条件if (response.status == 200) & else if (response.status == 304)

【讨论】:

  • 我已经改了,谢谢!问题是 get 返回 200 即使 "success" : "no" 。我怎样才能为这个“成功”设置条件?
  • 这不是状态和响应不匹配的原因。它是决定发送什么状态和响应的后端。但是你指出了一个很好的错误。
  • 所以你的意思是“status = 200”是由后端发送的,即使“没有成功”?因为后端是别人写的
  • 是的,status=200 表示您已成功收到后端的响应。没说成功与否
  • 是的,我必须处理 error_codes 而不是使用 response.data.error_code 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多