Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkStatus(response) {
  2.     // Throws error if http status is not success
  3.     // Assumes the request body is the error message
  4.     if (response.status >= 200 && response.status < 300) {
  5.       return response;
  6.     } else {
  7.       return response.text().then(function (body) {
  8.         throw new Error(body);
  9.       });
  10.     }
  11. }
  12.  
  13. function postItem(url) {
  14.   // posts an Item to URL, with the data in the form #item_edit_form
  15.   // this can be used for new Items, or to update an item, since they use the same form
  16.   fetch(url, {
  17.     credentials: 'same-origin', //send cookies
  18.     method: 'post',
  19.     body: new FormData($("#item_edit_form"))
  20.   })
  21.   .then(checkStatus)
  22.   .then(function(response) {
  23.     return response.text();
  24.   })
  25.   .then(function(body) {
  26.     if ( body == 'ok'){
  27.       // item saved successfully
  28.       Navigation.goBack();
  29.     } else {
  30.       // re-render the form with HTML received from server
  31.       // most likely there are validation errors
  32.       $(".page-content").innerHTML = body;
  33.       componentHandler.upgradeDom();
  34.     }
  35.   })
  36.   .catch(function(err) {
  37.     // unexpected error when requesting data
  38.     alert('Request failed: ' + err.message);
  39.   });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement