Answers for "AJAX SEND DATA PHP W ITH METHOD POST"

5

how to send data using ajax in php

$.ajax({
	url: "/something", // the url we want to send and get data from
    type: "GET", // type of the data we send (POST/GET)
    data: {p1: "This is our data"}, // the data we want to send
    success: function(data){ // when successfully sent data and returned
    	// do something with the returned data
   		console.log(data);
    }
}).done(function(){
	// this part will run when we send and return successfully
    console.log("Success.");
}).fail(function(){
    // this part will run when an error occurres
    console.log("An error has occurred.");
}).always(function(){
    // this part will always run no matter what
  	console.log("Complete.");
});
Posted by: Guest on November-26-2020
2

javascript send post data with ajax

function makeRequest (method, url, data) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    if(method=="POST" && data){
        xhr.send(data);
    }else{
        xhr.send();
    }
  });
}

//POST example
var data={"person":"john","balance":1.23};
makeRequest('POST', "https://www.codegrepper.com/endpoint.php?param1=yoyoma",data).then(function(data){
              var results=JSON.parse(data);
});
Posted by: Guest on August-02-2019

Code answers related to "Javascript"

Browse Popular Code Answers by Language