我试图从一个简单的ajax请求中获取值,但我不明白该怎么做.这是代码:
Rx.Observable .ajax({ url: 'https://jsonplaceholder.typicode.com/posts',method: 'GET',responseType: 'json' }) .subscribe(function(data) { return data.response; });
我到处搜索,没有简单的解释.
谢谢!
Observable.ajax可以使用以下接口接受字符串或对象:
原文链接:https://www.f2er.com/ajax/160181.html06000
see 07000
以下是示例:
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,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>`; });
<script src="https://unpkg.com/rxjs/bundles/rxjs.umd.min.js"></script> <div id="root">loading ...</div>