$ http发布回复错误,状态为302

时间:2015-07-18 16:58:57

标签: angularjs http xmlhttprequest

帖子的返回通过错误回调而下降但是我期待的json是由api调用返回的。当我在console.log中显示错误对象时,它是可见的。我会在代码后发布错误日志。

有效负载(userCreds)是json。使用postman,api调用按预期返回

这是帖子:

    var deferred = $q.defer();

   $http.post(url, userCreds)
            .then(function(data, status) {
                $log.info('validateLogin status: ' + status)

                if(data){
                    requestor = data;
                }
                deferred.resolve(requestor);
            }, function (data, status) {
                var error =  data || "Request failed";
                $log.error('validateLogin error: ' + JSON.stringify(error));
                deferred.reject(error);
            });

     return deferred.promise;

错误对象在其中具有正确的响应,状态为302。我注意到transformRequest和transformResponse为null。我不记得过去必须定义这些。我认为angular会在转换过程中自动处理字符串和javascript对象。

  {"data":{"$id":"1","innCodes":[],"userTypeId":0,"formId":0,"onqUserId":null,"fullName":"User Smith","firstName":"User","lastName":"Smith","phone":"214-555-4450","email":"user@email.com","userId":null,"password":null,"title":"Project Manager","fax":null,"mobile":null,"role":null},"status":302,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"url":"http://localhost:25396/api/user/ValidateCredentials/","data":{"userName":"userid1234","password":"pwd123456"},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Found"}

1 个答案:

答案 0 :(得分:0)

结束了导致奇怪行为的有效api响应302(找到)。我能够将状态代码更改为200(确定),现在一切都按预期工作。 302状态导致有角度拒绝承诺。

这行代码的角度为v 1.2.26

  return (isSuccess(response.status))
      ? resp
      : $q.reject(resp);