Guest User

Untitled

a guest
Jun 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. app.post('/api/update', (req, res) => {
  2. res.send(req.body);
  3. });
  4.  
  5. const HTTPClient = (method, url, post) => {
  6. return new Promise((resolve, reject) => {
  7.  
  8. const xhr = new XMLHttpRequest();
  9. xhr.open(method, url, true);
  10.  
  11. xhr.onload = () => {
  12. const statusText = xhr.statusText || '';
  13.  
  14. // responseText is the old-school way of retrieving response (supported by IE9)
  15. // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
  16. const response = ('response' in xhr) ? xhr.response : xhr.responseText;
  17.  
  18. // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
  19. let status = xhr.status === 1223 ? 204 : xhr.status;
  20.  
  21. // fix status code when it is 0 (0 status is undocumented).
  22. // Occurs when accessing file resources or on Android 4.1 stock browser
  23. // while retrieving files from application cache.
  24. if (status === 0) {
  25. status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
  26. }
  27. try {
  28. return resolve(JSON.parse(response));
  29. } catch (error) {
  30. return resolve(response);
  31. }
  32. };
  33.  
  34. xhr.onerror = () => reject('Error');
  35. xhr.ontimeout = () => reject('Timeout');
  36. xhr.onabort = () => reject('Abort');
  37.  
  38. xhr.send(JSON.stringify(post) || null);
  39. });
  40. };
  41.  
  42.  
  43. HTTPClient('post', '/api/update', defaultData).then(response => {
  44. css.value = response.data;
  45. console.log(response);
  46. }, error => {
  47. console.error(error);
  48. });
Add Comment
Please, Sign In to add comment