angular2 post以“application/x-www-form-urlencoded”形式传参的解决办法

以前没接触过angular1.x,现在用刚angular2的时候做项目,由于后台SpringMVC接收参数的认形式是application/x-www-form-urlencoded,angular1.x和2.0认都是application/json形式,所以后台接收不到参数,之前项目中,我都是在SpringMVC中增加额外代码让它对json的参数进行自适应,这么做就比较影响性能和有点坑(post单个参数和对象参数不一致),现在经过个把月对angular2的学习,这个解决起来很容易,(1.x网上一搜一大把,2.0的还没有,特此贴出)。

代码如下:
authenticate(data) {
  var username = data.credentials.username;
  var password = data.credentials.password;

  var creds = "username=" + username + "&password=" + password;

  var headers = new Headers();
  headers.append('Content-Type','application/x-www-form-urlencoded');

  this.http.post('http://localhost:3001/sessions/create',creds,{
    headers: headers
    })
    .map(res => res.json())
    .subscribe(
      data => this.saveJwt(data.id_token),err => this.logError(err),() => console.log('Authentication Complete')
    );
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...