Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript promise with ajax

function doTheThing() {
  $.ajax({
    url: window.location.href,
    type: 'POST',
    data: {
      key: 'value',
    },
    success: function (data) {
      console.log(data)
    },
    error: function (error) {
      console.log(error)
    },
  })
}
Comment

javascript promise with ajax

doTheThing()
doSomethingElse()
Comment

javascript pure ajax promise

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 :: array.includes is not a function react 
Javascript :: dynamic copyright year JavaScript centre aligned 
Javascript :: Use ChainLink Feed Registry 
Javascript :: add google maps nuxt js 
Javascript :: There is only one value in JavaScript that is not equal to itself, and that is NaN (“not a number”). 
Javascript :: limit frontend request 
Javascript :: flutter enum to json 
Javascript :: check if element is displayed 
Javascript :: 00979fb28f7cc517ff28c035bb8ef10698d0d991304e9901ba214ad0cada69ef:script- 
Javascript :: c# to json online 
Javascript :: cd doesn’t work inside childProcess 
Javascript :: create extern to be usable in c# 
Javascript :: Keyframe Overshoot 
Javascript :: imleç 
Javascript :: js The equivalent of destructuring arrays and objects 
Javascript :: convert low high to integer in js 
Javascript :: Variable As Parameter In Self Invoking Function 
Javascript :: How to switch to a remote git branch that does not exist locally 
Javascript :: wind in mongoose 
Javascript :: find parent index of nested array object javascript 
Javascript :: javascript code for adding scroll to top of page 
Javascript :: how to iterate through linked list javascript 
Javascript :: ticket 
Javascript :: check if a specific user is banned discord js 
Javascript :: how to prevent screen tearing without vsync 
Javascript :: delete all document fragments js 
Javascript :: react js public folder image path search 
Javascript :: node_modules is not generated in docker 
Javascript :: js to jsx 
Javascript :: phaser matter is undefined 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =