ajax
Crea un Observable para una petición Ajax
Last updated
import { ajax } from "rxjs/ajax";
import { mergeAll } from "rxjs/operators";
const ghibliFilm$ = ajax
.getJSON("https://ghibliapi.herokuapp.com/films")
.pipe(mergeAll());
ghibliFilm$.subscribe(console.log);
/* Salida:
{ ...title: 'Castle in the Sky'... },
{ ...title: 'Grave of the Fireflies'... },
{ ...title: 'My Neighbor Totoro'... }...
*/import { ajax } from "rxjs/ajax";
const ghibliFilmWithHeaders$ = ajax({
url: "https://ghibliapi.herokuapp.com/films",
method: "GET",
headers: {
"Content-Type": "json",
},
body: {
message: "Mensaje personalizado, porque podemos ;)",
},
});
ghibliFilmWithHeaders$.subscribe(console.log);
// Salida: AjaxResponse {xhr: {}, request: {}...}import { from, of } from "rxjs";
import { ajax } from "rxjs/ajax";
import { catchError, mergeMap } from "rxjs/operators";
const filmId$ = of(
"58611129-2dbc-4a81-a72f-77ddfc1b1b49",
"2baf70d1-42bb-4437-b551-e5fed5a87abe"
);
function getGhibliFilm(id: string) {
return ajax.getJSON(`https://ghibliapi.herokuapp.com/films/${id}`);
}
filmId$.pipe(mergeMap((id) => getGhibliFilm(id))).subscribe(console.log);
// Salida: {...title: 'Castle in the Sky'...}, {...title: 'My Neighbor Totoro'...}import { ajax } from "rxjs/ajax";
import { map, catchError } from "rxjs/operators";
import { of } from "rxjs";
const obs$ = ajax(`https://api.github.com/users?per_page=5`).pipe(
map((userResponse) => console.log("users: ", userResponse)),
catchError((error) => {
console.log("error: ", error);
return of(error);
})
);import { ajax } from "rxjs/ajax";
import { map, catchError } from "rxjs/operators";
import { of } from "rxjs";
const obs$ = ajax.getJSON(`https://api.github.com/users?per_page=5`).pipe(
map((userResponse) => console.log("users: ", userResponse)),
catchError((error) => {
console.log("error: ", error);
return of(error);
})
);import { ajax } from "rxjs/ajax";
import { of } from "rxjs";
const users = ajax({
url: "https://httpbin.org/delay/2",
method: "POST",
headers: {
"Content-Type": "application/json",
"rxjs-custom-header": "Rxjs",
},
body: {
rxjs: "Hello World!",
},
}).pipe(
map((response) => console.log("response: ", response)),
catchError((error) => {
console.log("error: ", error);
return of(error);
})
);import { ajax } from "rxjs/ajax";
import { map, catchError } from "rxjs/operators";
import { of } from "rxjs";
const obs$ = ajax(`https://api.github.com/404`).pipe(
map((userResponse) => console.log("users: ", userResponse)),
catchError((error) => {
console.log("error: ", error);
return of(error);
})
);