ajax

·2023년 1월 5일
0

RxJS

목록 보기
5/9
const obs$ = ajax('https://api.github.com/users?per_page=5').pipe(
  map(userResponse => userResponse.response),
  catchError(error => {
    console.log('error: ', error);
    return of(error);
  })
);

url에 ajax를 사용하면 Response타입의 객체를 옵저버블을 반환받는다.

map을 사용해서 response객체의 response프로퍼티를 반환할 수 있다.

obs$.subscribe({
  next: value => console.log(value),
  error: err => console.log(err)
});

구독함수에서 response프로퍼티를 받아 사용할 수 있다.


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);
  })
);

ajax.getJSON를 사용하면 Response객체의 respone프로퍼티를 반환한다.


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);
  })
);
 
users.subscribe({
  next: value => console.log(value),
  error: err => console.log(err)
});

url에 값을 전달하고 응답객체를 받을 수 있다.

0개의 댓글