为什么在使用通用和后改变contentType中应该不会影响请求负载/表单数据的区别? [英] Why the difference in using common and post for changing the ContentType should affect the request payload / form data?

查看:315
本文介绍了为什么在使用通用和后改变contentType中应该不会影响请求负载/表单数据的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用下面的:

A)

angularApp.config(function ($httpProvider) {
  $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  $httpProvider.defaults.headers.common['Accept'] = 'application/json';
  $httpProvider.defaults.transformRequest = function(data) {
      if (data === undefined) {
          return data;
      }
      return $.param(data);
  }
});

B)

angularApp.config(function ($httpProvider) {
  $httpProvider.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
  $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  $httpProvider.defaults.headers.common['Accept'] = 'application/json';
  $httpProvider.defaults.transformRequest = function(data) {
      if (data === undefined) {
          return data;
      }
      return $.param(data);
  }
});

,然后当我使用 $ http.post $资源使用 POST

a)是我想要的,因为我得到这个在Chrome开发工具:

a) is what I want because I get this in the Chrome Dev Tools:

  Form Data:
    User[old_password]:xxx
    User[new_password]:yyyy
    User[new_password_confirm]:yyyy

b)是不是我想要的,因为我得到这个在Chrome开发工具:

b) is NOT what I want because I get this in the Chrome Dev Tools:

Request Payload:
User%5Bold_password%5D=xxx&User%5Bnew_password%5D=yyyy&User%5Bnew_password_confirm%5D=yyyy

这让我为难,因为我希望共同申请的一切,包括职位。

This puzzles me because I expect common to apply for everything including post.

和一个唯一的区别)b)是b)的

The only difference between a) and b) is b) has

  $httpProvider.defaults.headers.**common**['Content-Type'] = 'application/x-www-form-urlencoded';

我使用的角度 1.2.6

问题也会发生在 1.2.9

请指教。

推荐答案

在你的配置块类型的:

console.log($httpProvider.defaults.headers.post); 

// Object {Content-Type: "application/json;charset=utf-8"} 

正如你所看到的, $ HTTP 已经有一个帖子方法默认的标题。

As you can see, $http already has a default header for a post method.

当你声明,通用报头的 $ HTTP 将不会覆盖的方法与普通头特定的头文件。与此相反的是真实的,第一个共同的头被应用,则该方法的具体头覆盖它们(如果​​存在的话)。

When you declare common headers $http would not override the method specific headers with the common headers. The opposite is the true, first the common headers are applied then the method specific headers override them (if exist).

您可以重新设置默认值,像这样:

You can reset the defaults like so:

$httpProvider.defaults.headers.post = {};

http.js 可以看到标题是如何被合并:

Inside http.js you can see how headers are being merged:

function mergeHeaders(config) {
  var defHeaders = defaults.headers,
      reqHeaders = extend({}, config.headers),
      defHeaderName, lowercaseDefHeaderName, reqHeaderName;

  defHeaders = extend({}, defHeaders.common, defHeaders[lowercase(config.method)]);

在使用 Angular.extend ,后来对象的属性将覆盖任何属性preceding对象。

When using Angular.extend, the properties of later object overrides the properties of any preceding objects.

这篇关于为什么在使用通用和后改变contentType中应该不会影响请求负载/表单数据的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆