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);
});
Also in JavaScript:
- javascript get day of year from date
- nodejs format text
- how to make an image like canvas in node js
- reverse proxy using expressjs
- js select option value when selected
- data table in angular 8 from api
- or operator javascript
- multiple case switch javascript
- javascript object entries
- Xbonacci
- angular list contains property
- JsonConvert.DeserializeObject convert into dynamic datatable
- javascript write to file
- javascript string first letter lowercase
- function expression vs function declaration
- react native side drawer
- v-btn click
- heroku failed to run files from static folder in node
- regular expression flags
- jquery loop through each child element
- left_field in jsgrid
- js datetime format
- i18n react get current language
- online javascript compiler