Angular 2的HTTP请求未发送凭据。

5

我正在使用Angular 2,并尝试使用凭据发送HTTP请求:

这个Angular 1代码很好用,它包括了凭据:

$http.get("http://some.com/user",{ withCredentials: true})

根据API预览,我实施了这段Angular 2代码,但它不包括凭据:
    http.get("http://some.com/user", {
        headers: myHeaders,
        credentials: RequestCredentialsOpts.Include
    }).toRx()
        .map(res => res.json())
        .subscribe(
        // onNext callback
            data => this.successHandler(data),
        // onError callback
            err  => this.failure(err)
    );

我正在使用 Angular 2.0.0-alpha.37 进行工作。

RequestCredentialsOpts.Include已被移除,但目前还没有关于将来正确设置withCredentials的任何信息-> https://github.com/angular/angular/issues/4231 - Dani
回答我的问题。Angular2的主要开发人员之一已经解决了这个问题,我们将能够再次使用“withCredentials”。-> https://github.com/angular/angular/pull/5501 - Dani
1个回答

6

我正在使用 angular2@2.0.0-alpha.37;

如果您在xhr_backed.js中添加代码,您可以向服务器发送'credentials'。

this._xhr.withCredentials = true;

angular2@2.0.0-alpha.37/src/http/backends/xhr_backed.js

var XHRConnection = (function() {
  function XHRConnection(req, browserXHR, baseResponseOptions) {
    ........
    this._xhr = browserXHR.build();
    this._xhr.withCredentials = true;
    ........

:)


2
已被@robwormald修复 -> https://github.com/robwormald/angular/commit/547ea806b881969ded22d4c2880d403f0393bbab - Dani
实际上,这个简短的回答应该有很多赞。 - Silencer
@dani 不过这个 PR 已经关闭了 :( - jasonslyvia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接