以前没接触过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')
);
}