简体   繁体   English

将AngularJS $ http与asp.net网络服务一起使用,是否可以设置请求标头?

[英]Using AngularJS $http with asp.net webservice, is there a way to set the request headers?

I've just come across a bizarre issue with regards to retrieving data via asp.net webservice. 关于通过asp.net Web服务检索数据,我遇到了一个奇怪的问题。

when using JQuery's ajax method the headers are set correctly and the data is retrieved in JSON successfully. 使用JQuery的ajax方法时,标头设置正确,并且已成功在JSON中检索数据。

JSON example: JSON示例:

$.ajax({
    type: "GET",
    url: "service/TestService.asmx/GetTestData",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: callback,
    error: function (err, xhr, res) {
      alert(err);
    }
  });

The Request Headers for the above is the following: 以上的请求标头如下:

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type    application/json; charset=utf-8
Host    localhost
Referer http://localhost/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
X-Requested-With    XMLHttpRequest

The Reponse Headers for above is the following: 以上的响应标头如下:

Cache-Control   private, max-age=0
Content-Length  327
Content-Type    application/json; charset=utf-8
Date    Tue, 29 Oct 2013 17:59:56 GMT
Server  Microsoft-IIS/7.5
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET

this works fine. 这很好。

But for AngularJS $http method the Request Headers Content-Type value is not set, therefore the Response Headers Content-Type defaults to text/xml; charset=utf-8 但是对于AngularJS $ http方法,没有设置Request Headers Content-Type值,因此Response Headers Content-Type默认为text/xml; charset=utf-8 text/xml; charset=utf-8 . text/xml; charset=utf-8 Have a look at the example below: 看下面的例子:

$http({
    method : 'GET',
    url: 'service/TestService.asmx/GetTestData',
    headers: {
      'Accept': 'application/json, text/javascript, */*; q=0.01',
      'Content-Type': 'application/json; charset=utf-8'
    }
  }).success(callback);

The Request Headers for above is as follows, you will see that Content-Type is missing: 上面的请求标头如下,您将看到缺少Content-Type:

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host    localhost
Referer http://localhost/ComponentsAndRepos/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0

therefore the Response Headers for the above is the following: 因此,以上的响应标头如下:

Cache-Control   private, max-age=0
Content-Encoding    gzip
Content-Length  341
Content-Type    text/xml; charset=utf-8
Date    Tue, 29 Oct 2013 17:59:56 GMT
Server  Microsoft-IIS/7.5
Vary    Accept-Encoding
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET

therefore this forces the response to return as XML not JSON, is there a way to resolve this? 因此,这会强制响应以XML而不是JSON的形式返回,是否有解决方法?

thank you, 谢谢,

Update Thanks to Erstad Stephen 更新感谢Erstad Stephen

This has been resolved by adding data:{} property to $http method. 通过将data:{}属性添加到$http方法可以解决此问题。

    $http({
    method : 'GET',
    url: 'service/TestService.asmx/GetTestData',
    data: {},
    headers: {
      'Accept': 'application/json, text/javascript, */*; q=0.01',
      'Content-Type': 'application/json; charset=utf-8'
    }
  }).success(callback);

You can handle this a couple of different ways: 您可以通过两种不同的方式处理此问题:

  1. You can set the header defaults through the $httpProvider: http://docs.angularjs.org/api/ng .$http#description_setting-http-headers 您可以通过$ httpProvider设置标头默认值: http ://docs.angularjs.org/api/ng。$ http#description_setting-http-headers
  2. You can also use the Interceptors in Angular to intercept the idea for $http to modify the config object for all requests: http://docs.angularjs.org/api/ng .$http#description_interceptors 您还可以使用Angular中的Interceptors来拦截$ http的想法,以修改所有请求的配置对象: http : //docs.angularjs.org/api/ng。$ http#description_interceptors
  3. You could also set the config setting like you are above. 您也可以像上面一样设置配置设置。

The biggest thing is that you maybe misunderstanding how the config works. 最大的事情是您可能会误解配置的工作方式。 See this question here: Angular, content type is not being generated correctly when using resource 请在此处查看此问题: 使用资源时,无法正确生成角型内容类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM