如何读取带有角度的API发送的标头?

时间:2016-09-14 19:50:02

标签: c# angularjs http-headers

我在domain.com上有类似于以下代码:

$http.post("http://api.domain.com/Controller/Method",
    JSON.stringify(data),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(function (response) {
        console.log(response);
    }, function (response) {
        // something went wrong
    });
}

与我的.NET API进行良好的通信。 response.data包含我服务器需要提供的所有数据。但是我们有一个新的安全令牌,我们从API传递给客户端,我们将它传递回数据包头中的客户端。我知道令牌正在传回,因为我可以在chrome调试器的网络选项卡上的数据包中读取它。但是response.headers()仅包含content-type:"application/json; charset=utf-8"它不包含数据包中的内容。有谁有想法?

从API返回数据(C#) HttpContext.Current.Response.AppendHeader("session",Guid.NewGuid().ToString());

所以我希望response有一个名为session的标题,但事实并非如此。但它在数据包中。

5 个答案:

答案 0 :(得分:13)

成功使用headers变量和错误回调

$http.get('/url').
  success(function(data, status, headers, config) {
    //things to do
  })
  .error(function(data, status, headers, config) {
    //things to do
  });

答案 1 :(得分:0)

有两种方法可以处理$http调用,即.success.then。但是。很久以来,Angular不推荐使用.success,所以建议使用{{1}现在回答这个问题的是一个简单的GET调用的demo

.then

在此我设置了一个身份验证令牌来验证请求 $http.get('test.json').then(function(response) { $scope.collection = response.data.Collections; console.log(response); console.log( response.headers()); }); ,但如果我执行console.log(response.headers()),它看起来就像下面的

$http.defaults.headers.common['Auth-Token'] = '1234';

这不显示身份验证令牌,但在响应中还有一个名为cache-control : "no-cache" cf-ray : "2e3a4163cdf43084-SIN" content-encoding : "gzip" content-type : "application/json; charset=utf-8" date : "Sat, 17 Sep 2016 05:46:02 GMT" etag : ""1ae47a56e2b2ea9ddc982cc0e61e469a-static-gzip"" server : "cloudflare-nginx" status : "304" vary : "accept-encoding" x-xss-protection : "0" 的对象,该对象还包含config对象,其中包含我的身份验证令牌。尝试遵循此模式,希望这种方法为您提供问题陈述的新视图。

headers

答案 2 :(得分:0)

使用成功回调中返回的头文件方法。

$http.get('/url').
  success(function(data, status, headers, config) {
    response.headers = headers();
    console.log(response.headers, response.headers['auth_token']);
  })
  .error(function(data, status, headers, config) {
    //things to do
  });

不要忘记调用标题()。

答案 3 :(得分:0)

        // Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });


    The response object has these properties:

    data – {string|Object} – The response body transformed with the transform functions.
    status – {number} – HTTP status code of the response.
    headers – {function([headerName])} – Header getter function.
    config – {Object} – The configuration object that was used to generate the request.
    statusText – {string} – HTTP status text of the response.
    A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code.

答案 4 :(得分:0)

$http.post("http://api.domain.com/Controller/Method",
    JSON.stringify(data),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(function (response) {
        console.log(response);
        //this will get you session header information
        console.log( response.headers('session'));

    }, function (response) {
        // something went wrong
    });
}