“JavaScript Pure Ajax” Code-Antworten

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();
    }
  );
}
Lucky Llama

JS funktionale AJAX -Anfragen

function checkUserIdExists(userid, callback) {
        return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        }
    })
    .done(callback)
    .fail(function(jqXHR, textStatus, errorThrown) {
        // Handle error
    });
}

checkUserIdExists(2, function(data) {
    console.log(data); // Do what you want with the data returned
});
Xerothermic Xenomorph

Ähnliche Antworten wie “JavaScript Pure Ajax”

Fragen ähnlich wie “JavaScript Pure Ajax”

Weitere verwandte Antworten zu “JavaScript Pure Ajax” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen