Rxjs 5 - 简单的Ajax请求

Mar*_*ark 13 ajax rxjs rxjs5

我试图从一个简单的ajax请求中获取值,但我不明白该怎么做.这是代码:

Rx.Observable
  .ajax({ url: 'https://jsonplaceholder.typicode.com/posts', method: 'GET', responseType: 'json' })
  .subscribe(function(data) { return data.response; });
Run Code Online (Sandbox Code Playgroud)

我到处搜索,没有简单的解释.

谢谢!

Ole*_*luk 25

Observable.ajax可以接受stringObject使用以下界面:

interface AjaxRequest {
  url?: string; // URL of the request
  body?: any;  // The body of the request
  user?: string; 
  async?: boolean; // Whether the request is async
  method?: string; // Method of the request, such as GET, POST, PUT, PATCH, DELETE
  headers?: Object; // Optional headers
  timeout?: number;
  password?: string;
  hasContent?: boolean;
  crossDomain?: boolean; //true if a cross domain request, else false
  withCredentials?: boolean;
  createXHR?: () => XMLHttpRequest; //a function to override if you need to use an alternate XMLHttpRequest implementation
  progressSubscriber?: Subscriber<any>;
  responseType?: string;
}
Run Code Online (Sandbox Code Playgroud)

请参阅GitHub上的AjaxObservable.ts

以下是示例:

const { Observable, combineLatest } = rxjs; // = require("rxjs")
const { ajax } = rxjs.ajax; // = require("rxjs/ajax")
const { map } = rxjs.operators; // = require("rxjs/operators")

// simple GET request example
const simple$ = ajax('https://httpbin.org/get');

// POST request example
const complex$ = ajax({
  url: 'https://httpbin.org/post',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-rxjs-is': 'Awesome!'
  },
  body: {
    hello: 'World!',
  }
});

const htmlSubscription = combineLatest(simple$, complex$)
  .subscribe(([simple, complex]) => {
    const simpleResponse = JSON.stringify(simple.response, null, 2);
    const complexResponse = JSON.stringify(complex.response, null, 2);
    document.getElementById('root').innerHTML = `
      <div>
        <span><b>GET</b> https://httpbin.org/get</span>
        <pre>${simpleResponse}</pre>

        <span><b>POST</b> https://httpbin.org/post</span>
        <pre>${complexResponse}</pre>
      </div>`;
  });
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/rxjs@6.2.2/bundles/rxjs.umd.min.js"></script>
<div id="root">loading ...</div>
Run Code Online (Sandbox Code Playgroud)

  • 快速问题有谁知道RxJS是否可以进行Ajax调用然后为什么angular有自己的http lib? (2认同)

Ane*_*Dev 5

打字稿版本

"dependencies": { 
     "rxjs": "^5.1.0"
 }
Run Code Online (Sandbox Code Playgroud)

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/dom/ajax';
import 'rxjs/add/observable/combineLatest';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/catch';


 const posts$ = Observable
  .ajax('https://jsonplaceholder.typicode.com/posts')
  .map(e => e.response);


  const htmlSubscription = posts$
  .subscribe(res => {
   console.log(JSON.stringify(res, null, 2));
  });
Run Code Online (Sandbox Code Playgroud)