【问题标题】:$http post response errors with 302 status$http 发布响应错误,状态为 302
【发布时间】:2015-10-08 05:10:13
【问题描述】:

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

有效载荷 (userCreds) 是 json。使用邮递员,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 为空。我不记得过去必须定义这些。我认为 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"}

【问题讨论】:

  • 我认为这里没有足够的信息来诊断您的问题。在不相关的说明中,您正在使用承诺,并且会从阅读此 stackoverflow.com/questions/23803743/… 中受益
  • 感谢@m59 的回复,我会检查那个帖子。还需要什么信息?我列出了调用方法和响应。预期的响应对象在“数据”对象中可见——即从 api 调用返回的 json。

标签: angularjs http xmlhttprequest


【解决方案1】:

最终成为导致古怪行为的有效 api 响应 302(已找到)。我能够将状态代码更改为 200(OK),现在一切都按预期工作。 302 状态导致 Angular 拒绝承诺。

Angular v 1.2.26 中的这行代码

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

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2018-05-27
    • 2015-12-29
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2020-03-15
    相关资源
    最近更新 更多