angularjs返回json数据空白没有返回错误消息

时间:2015-01-14 13:44:21

标签: json angularjs

我有以下代码......一切正常。我正在测试当用户输入无效的用户ID /密码时404回来并想要拦截我带回来的消息......这里是角度代码。我在哪里/如何获得我在api控制器中写的httpresponsemessage?数据显示为空白.... webapi代码也会有帮助吗?

DataFactory.checkPW(userid, password).success(function()
{
  (function(data, status, headers, config)
    {
        //do stuff
    }
  )();
}).error(function(data, status, headers, config)
{
  //data shows up undefined
  //status shows 404 correctly
  //header information shows correctly
  //config shows up undefined

});

1 个答案:

答案 0 :(得分:0)

有两种拦截器(以及两种拒绝拦截器):

request: interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object.
requestError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
response: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.
responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config;
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    },



    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    }
  };
});

添加拦截器的代码:

module.config(['$httpProvider', function($httpProvider) {  
    $httpProvider.interceptors.push('myHttpInterceptor');
}]);

这样您就可以自定义$ http请求和响应。