Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GET than - catch
- -----------------------
- fetch(BASE_URL)
- .then((response) => response.json())
- .then((data) => {
- console.log(data)
- })
- .catch((error) => console.error(error));
- // GET async - await
- -----------------------
- let response = await fetch(BASE_URL);
- let data = await response.json();
- ************************************************************************************************
- // POST than - catch
- -----------------------
- fetch(BASE_URL, {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({ author, title })
- })
- // const payload = JSON.stringify({
- // product: product.value,
- // count: count.value,
- // price: price.value
- // });
- // const httpHeaders = {
- // method: 'POST',
- // body: payload
- // };
- // fetch(BASE_URL, httpHeaders)
- // .then(() => {
- // loadAllProductsHandler();
- // otherDOMSelectors.form.reset();
- // })
- // .catch((err) => {
- // console.error(err);
- // })
- // }
- // POST async - await
- -----------------------
- await fetch(BASE_URL, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- });
- ************************************************************************************************
- // PUT than - catch
- -----------------------
- fetch(`${BASE_URL}${id}`, {
- method: 'PUT',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({
- "author": changedAuthor,
- "title": changedTitle
- })
- ************************************************************************************************
- // PATCH async -await
- -----------------------
- await fetch(BASE_URL + id, {
- method: 'PATCH',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- })
- // PATCH than - catch
- -----------------------
- const payload = JSON.stringify({
- product: product.value,
- count: count.value,
- price: price.value
- });
- const httpHeaders = {
- method: 'PATCH',
- body: payload
- }
- fetch(`${BASE_URL}${productToEdit._id}`, httpHeaders)
- .then(() => {
- loadAllProductsHandler();
- otherDOMSelectors.addBtn.removeAttribute('disabled');
- otherDOMSelectors.updateBtn.setAttribute('disabled', true);
- otherDOMSelectors.form.reset();
- })
- .catch((err) => {
- console.error(err);
- })
- }
- ************************************************************************************************
- // DELETE than - catch
- -----------------------
- fetch(`${BASE_URL}${id}`, {
- method: 'DELETE'
- })
- // function deleteProductHandler() {
- // const id = this.parentNode.parentNode.id;
- // const httpHeaders = {
- // method: 'DELETE'
- // };
- // fetch(`${BASE_URL}${id}`, httpHeaders)
- // .then(() => loadAllProductsHandler())
- // .catch((err) => {
- // console.error(err);
- // })
- // }
- // DELETE async - await
- -----------------------
- await fetch(BASE_URL + id, {
- method: 'DELETE'
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement