Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript pure ajax

function getRequest(url) {
    return makeRequest('GET', url);
}

function postRequest(url, data) {
   return makeRequest('POST', url, data);
}

function makeRequest(method, url, data) {
  return new Promise(
    function(resolve, reject) {
      var http = new XMLHttpRequest();
      http.open(method, url);
      http.onload = function() {
        if (this.status >= 200 && this.status < 300) {
          var response = http.response;
          try {
            response = JSON.parse(response);
            resolve(response);
          } catch (error) {
            reject({
              status: this.status,
              statusText: error
            });
          }

        } else {
          reject({
            status: this.status,
            statusText: http.statusText
          });
        }
      };
      http.onerror = function() {
        reject({
          status: this.status,
          statusText: http.statusText
        });
      };

      if (method === 'POST') {
        data = data || new FormData();
        http.send((data));
      } else http.send();
    }
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to Use the trim() String Method in javascript 
Javascript :: javascript push and concat 
Javascript :: null + undefined 
Javascript :: this.props undefined react native 
Javascript :: first letter string uppercase javascript 
Javascript :: Accessing Object Properties with Variables 
Javascript :: jquery select selected get data name 
Javascript :: how to create a point in js 
Javascript :: datatables modify rows 
Javascript :: check number javascript 
Javascript :: update a certain key in dictionary javascript 
Javascript :: react-native restart app 
Javascript :: Check the render method of `App` 
Javascript :: discord.js dm all members 
Javascript :: javascript timestamp conversion 
Javascript :: aimbot scripts island royale 
Javascript :: add a socket to a room in socket.io 
Javascript :: javascript count up timer 
Javascript :: clear session on browser close javascript 
Javascript :: javascript quick float to integer 
Javascript :: javascript time difference 
Javascript :: framer motion for react 
Javascript :: javascript array loop 
Javascript :: react native dynamically update flatlist data 
Javascript :: new date getday js 
Javascript :: angular decorators list 
Javascript :: navbar route with params vue 
Javascript :: how the concat function works javascript 
Javascript :: check if element with class has another class javascript 
Javascript :: vscode add shortcut to run in terminal 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =