JSFiddle - React, Tailwind, and code Playground
JavaScript
async function someOfYourWebsiteScript(){
async function ajaxRequest(url = '', data = {}, method = 'get') {
// the server should response content type of application/json header
return $.ajax({
method: method,
url: url,
contentType: "application/json",
data: data,
complete: function (response) {
let res = response.responseText;
return res;
},
error: function (err) {
alert('an error accourd!');
return false;
},
});
}
// it would be better if you can avoid JSON.stringify
let realPostData = {
"first_name": "James",
"last_name": "Bond",
};
// we are using stringify only beacuse this is how jsfiddle.net need an object from me
let demoPostData = {
json: JSON.stringify(realPostData),
};
ajaxRequest("/echo/json/", demoPostData, 'POST').then(result => {
console.log('Your first name is: ' + result.first_name);
console.log('Your last name is: ' + result.last_name);
});
ajaxRequest("https://httpbin.org/ip").then(result => {
console.log('Your PC Address is:' + result.origin);
});
let myPcRes = await ajaxRequest("https://httpbin.org/ip");
console.log('Your PC Address is from the await approach: ' + myPcRes.origin);
}
someOfYourWebsiteScript();