how to send data in ajax data code example
Example 1: how to send data using ajax
$.ajax({
url: "/something",
type: "GET",
data: {p1: "This is our data"},
success: function(data){
console.log(data);
}
}).done(function(){
console.log("Success.");
}).fail(function(){
console.log("An error has occurred.");
}).always(function(){
console.log("Complete.");
});
Example 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();
}
});
}
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);
});