Skip to content

Instantly share code, notes, and snippets.

@vladimir-ivanov
Last active February 19, 2016 08:58
Show Gist options
  • Select an option

  • Save vladimir-ivanov/4e442070e483fd1ed00d to your computer and use it in GitHub Desktop.

Select an option

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
/// <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();
}
}
@vladimir-ivanov

Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment