我正在使用带有弹簧支架的 angular2-rc4。我正在使用弹簧身份验证。
Spring 认证期望 content-type 为 application/x-www-form-urlencoded;charset=UTF-8 并以纯字符串格式发布数据,例如“j_username=user&j_password=pass”。
所以我写了以下代码:
let data = 'j_username=user&j_password=password'; //Spring authentication expects data in this format.
let headers = new Headers({'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'});
let options = new RequestOptions({headers: headers});
this.http.post(url, data, options).subscribe(
.....
....
);
当我通过firefox执行这段代码时,它没有任何问题。数据正在到达服务器并且身份验证工作正常。
但是当我从 chrome 开始时,它不起作用。用户名和密码以空值到达服务器。
我在firebug的帮助下比较了chrome和firefox的请求头。我注意到一个不同之处。内容类型在 Firefox 中正常运行,但在 chrome 中不正常。下面是 chrome 和 firefox 的内容类型:
Firefox : Content-type: application/x-www-form-urlencoded; charset=UTF-8
Chrome:内容类型:test/plain,application/x-www-form-urlencoded;字符集=UTF-8
如果您看到 chrome 会自动将 text/plain 附加到其标题。我不明白这背后的原因。
注意:此代码与 angular2-rc1 一起使用。升级到 rc4 后开始面临问题。
请帮忙
谢谢