Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fin sección 8

Choose a tag to compare

@alesyt0h alesyt0h released this 01 Oct 18:54
· 4 commits to main since this release

AJAX

  • ajax: Peticiones AJAX en RxJS import { ajax } from 'rxjs/ajax';
ajax(url);
// Same as 
ajax.get(url);

// Headers
ajax(url, {
    'token': 'auth123'
});
  • getJSON: Devuelve el body de la petición en formato JSON directamente, sin tener que pasarlo por el map o el pluck: ajax.getJSON(url);
  • catchError: Un catchError debe retornar siempre un observable, se puede conseguir mediante un return of(false), of([]), of({})... etc
  • AjaxError: Tipado para el error de una petición AJAX
  • CRUD: POST, PUT and DELETE. (Una petición DELETE devolvería un error en caso de incluirse el body tal y como está en el ejemplo)
ajax.post(url, {
    id: 1,
    nombre: 'Alejandro'
}, {
    'mi-token': 'ABC123'
}).subscribe(console.log);
  • CRUD: Método reutilizable: (En el caso que fuera una petición DELETE no devolvería error, se ejecutaría correctamente)
ajax({
    url,
    method: 'DELETE',
    headers: {
        'mi-token': 'ABC123'
    },
    body: {
        id: 1,
        nombre: 'Alejandro'
    }
}).subscribe(console.log);