如何通过rxjs ajax发出FORM PUT请求?

时间:2017-03-20 20:28:01

标签: ajax rxjs5

Jquery请求对象有一个名为'data'的属性,用于设置表单数据并使用正确的标题发出表单请求。

我想在rxjs ajax中实现相同但无法找到方法。 我能找到的唯一详尽的请求配置属性列表是https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/operators/ajax.md

1 个答案:

答案 0 :(得分:2)

RxJS5的文档尚未完成,但您可以按如下方式使用它:

 import {Observable} from "rxjs/Observable";
 import "rxjs/add/observable/dom/ajax";

 const url = "http://my-site.org/some/endpoint";
 const request$ = Observable.ajax.put(url, {a: "test"});
 request$.subscribe(x => console.log(x));

当然所有其他REST方法都可用

  Observable.ajax(urlOrRequest: string | AjaxRequest): Observable<AjaxResponse>;
  Observable.ajax.get(url: string, headers?: Object): Observable<AjaxResponse>;
  Observable.ajax.post(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  Observable.ajax.put(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  Observable.ajax.patch(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  Observable.ajax.delete(url: string, headers?: Object): Observable<AjaxResponse>;
  Observable.ajax.getJSON<T>(url: string, headers?: Object): Observable<T>;

如果要发送完全自定义的ajax请求,可以使用以下界面:

interface AjaxRequest {
  url?: string;
  body?: any;
  user?: string;
  async?: boolean;
  method?: string;
  headers?: Object;
  timeout?: number;
  password?: string;
  hasContent?: boolean;
  crossDomain?: boolean;
  withCredentials?: boolean;
  createXHR?: () => XMLHttpRequest;
  progressSubscriber?: Subscriber<any>;
  responseType?: string;
}

所有人都从这里开始:https://github.com/ReactiveX/rxjs/blob/0ab1d3b4d1178a1d31c49c737832cde767da3fb1/src/observable/dom/AjaxObservable.ts