1

我正在使用带有弹簧支架的 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 后开始面临问题。
请帮忙
谢谢

4

1 回答 1

0

自 2 天以来,我一直在为这个问题苦苦挣扎,在发布这个问题之后,我得到了 angular2-rc4 代码的解决方案。我在这里发布解决方案,以便它可以帮助其他人。
我更改了我的代码,如下所示,它开始工作: 来自:let headers = new Headers({'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}); To: 让 headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

但我仍然不确定以前的代码在 Firefox 中是如何工作的。

于 2016-07-15T08:08:06.877 回答