Last active
February 19, 2016 08:58
-
-
Save vladimir-ivanov/4e442070e483fd1ed00d to your computer and use it in GitHub Desktop.
observable wrapper for angular2 Http, using jQuery as a temporary solution to allow withCredentials property on xhr object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <reference path='../../typings/angularjs/angular.d.ts' /> | |
| //TODO - a temp fix is using jquery ajax until angular2/http adds support for withCredentials on their xhr objects | |
| import {Http, Headers} from 'angular2/http'; | |
| import {Injectable, Observable} from 'angular2/core'; | |
| @Injectable() | |
| export class ObservableRequestService { | |
| constructor(private http:Http) { | |
| } | |
| get(relativePath:string, formatter:Function = null):Observable<any> { | |
| return this.http.get(relativePath).map(this.mapper); | |
| } | |
| delete(relativePath:string, details) { | |
| return this.doRequest('DELETE', relativePath, details); | |
| } | |
| post(relativePath:string, details) { | |
| return this.doRequest('POST', relativePath, details); | |
| } | |
| private doRequest(method, relativePath, data):Observable<any> { | |
| return Observable.fromPromise($.ajax({ | |
| url: relativePath, | |
| method: method, | |
| contentType: 'application/json', | |
| data: JSON.stringify(data), | |
| xhrFields: { | |
| withCredentials: true | |
| } | |
| })).map(this.mapper); | |
| } | |
| private mapper(response:{json: Function}) { | |
| return response.json(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/angular/http/issues/65
The above is more of an experiment with Observables (which can also be done with window.fetch and polyfills etc).
Best approach considering we are in angular2 world is to use the workaround:
https://github.com/angular/http/issues/65#issuecomment-181560171