Angular 2-http getwithCredentials [英] Angular 2 - http get withCredentials

查看:105
本文介绍了Angular 2-http getwithCredentials的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些与此相关的内容,但是大多数示例和说明已被弃用,并且不适用于RC1.

I found something regarding this, but most examples and explanations are deprecated and it's not applicable to RC1.

import {Injectable} from "@angular/core";
import {Http, HTTP_PROVIDERS, Response, RequestOptions, URLSearchParams} from "@angular/http";
import 'rxjs/add/operator/map';
@Injectable()
export class AuthService {
constructor( private _http: Http ) {}
GetLoggedUser(){
    return this._http.get('http://dev/api/v1/current-user')
     .map((res:Response) => res.json())
} 
} 

我需要完全按照此旧版代码进行调用:

I need to make this call exactly as this legacy code:

$(document).ready(function() {
                    jQuery.ajax({
                        type: 'GET',
                        url: 'http://dev/api/v1/current-user',
                        xhrFields: {
                            withCredentials: true
                        },
                    }).done(function(data) {
                        $('#user').html(JSON.stringify(data));
                    });
                });

因此,基本上,我需要使用withCredentials进行通话.有帮助吗?

So, basically I need to make the call using withCredentials. Any help ?

推荐答案

使用RC1,您需要扩展BrowserXhr类:

With RC1 you need to extend the BrowserXhr class:

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
  constructor() {}
  build(): any {
    let xhr = super.build();
    xhr.withCredentials = true;
    return <any>(xhr);
  }
}

,并使用扩展名覆盖BrowserXhr提供程序:

and override the BrowserXhr provider with the extended:

bootstrap(AppComponent, [
  HTTP_PROVIDERS,
  provide(BrowserXhr, { useClass: CustomBrowserXhr })
]);

在即将推出的RC2中,您将能够在请求选项中使用withCredentials属性.

With the upcoming RC2, you'll be able to use a withCredentials attribute in request options.

有关更多详细信息,请参见以下链接:

See these links for more details:

  • http://5thingsangular.github.io/2016/05/30/issue-6.html
  • https://github.com/angular/angular/pull/7281

这篇关于Angular 2-http getwithCredentials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆