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

时间:2021-02-22 22:50:56

I have something similar to the following code on domain.com:

我在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
    });
}

It works great communicating with my .NET API. response.data has all of the data my server needs to give me. However we have a new security token that we are passing to the client from the API and we are passing it in back to the client in the packet header. I know that the token is being passed back because I can read it in the packet on the network tab in chrome debugger. However response.headers() only contains content-type:"application/json; charset=utf-8" It doesn't have what is in the packet. Any one have an idea?

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

The data is returned from the API like so (C#) HttpContext.Current.Response.AppendHeader("session",Guid.NewGuid().ToString());

数据从API返回,如此(C#)HttpContext.Current.Response.AppendHeader(“session”,Guid.NewGuid()。ToString());

So i would expect response to have a header called session, but it does not. It is however in the packet.

所以我希望响应有一个名为session的头,但事实并非如此。然而它在数据包中。

5 个解决方案

#1


13  

Use the headers variable in success and error callbacks

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

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

#2


0  

There are two ways of handling $http call i.e .success and .then.But .success is deprecated by Angular since a long time, so it is recommended to use .then.Now to answer the question here is the demo of a simple GET call

有两种方法可以处理$ http调用,即.success和.then.But.success很久以来一直被Angular弃用,因此建议使用.then.Now来回答这里的问题是简单GET的演示呼叫

$http.get('test.json').then(function(response) {
      $scope.collection = response.data.Collections;
      console.log(response);
      console.log( response.headers());
    });

in this I've set an authentication token to validate the request $http.defaults.headers.common['Auth-Token'] = '1234';,but if I do console.log(response.headers()) it looks like below

在这个我设置了一个身份验证令牌来验证请求$ http.defaults.headers.common ['Auth-Token'] ='1234';,但是如果我做console.log(response.headers())它看起来如下

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"

This doesn't show the authentication token,but in the response there is also an object called config which also has headers object which contains my authentication token.Try following this pattern,hope this approach gives you a new view on your problem statement.

这不会显示身份验证令牌,但在响应中还有一个名为config的对象,它还包含包含我的身份验证令牌的头对象。尝试遵循此模式,希望此方法为您提供有关问题陈述的新视图。

config : Object
       L->  headers: Object
             L->     Accept : "application/json, text/plain, */*"
                     Auth-Token : "1234"

#3


0  

Use headers method returned in success callback.

使用成功回调中返回的header方法。

$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
  });

Do not forget to call headers().

不要忘记调用headers()。

#4


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.

#5


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
    });
}

#1


13  

Use the headers variable in success and error callbacks

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

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

#2


0  

There are two ways of handling $http call i.e .success and .then.But .success is deprecated by Angular since a long time, so it is recommended to use .then.Now to answer the question here is the demo of a simple GET call

有两种方法可以处理$ http调用,即.success和.then.But.success很久以来一直被Angular弃用,因此建议使用.then.Now来回答这里的问题是简单GET的演示呼叫

$http.get('test.json').then(function(response) {
      $scope.collection = response.data.Collections;
      console.log(response);
      console.log( response.headers());
    });

in this I've set an authentication token to validate the request $http.defaults.headers.common['Auth-Token'] = '1234';,but if I do console.log(response.headers()) it looks like below

在这个我设置了一个身份验证令牌来验证请求$ http.defaults.headers.common ['Auth-Token'] ='1234';,但是如果我做console.log(response.headers())它看起来如下

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"

This doesn't show the authentication token,but in the response there is also an object called config which also has headers object which contains my authentication token.Try following this pattern,hope this approach gives you a new view on your problem statement.

这不会显示身份验证令牌,但在响应中还有一个名为config的对象,它还包含包含我的身份验证令牌的头对象。尝试遵循此模式,希望此方法为您提供有关问题陈述的新视图。

config : Object
       L->  headers: Object
             L->     Accept : "application/json, text/plain, */*"
                     Auth-Token : "1234"

#3


0  

Use headers method returned in success callback.

使用成功回调中返回的header方法。

$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
  });

Do not forget to call headers().

不要忘记调用headers()。

#4


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.

#5


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
    });
}