This JavaScript function will send an AJAX request – GET
or POST
– with no third-party dependencies:
function soQuery(soMethod, soUri) {
var xhttp = new XMLHttpRequest();
xhttp.open(soMethod, soUri, true);
if (soMethod === 'GET') {
xhttp.send();
} else if (soMethod === 'POST') {
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.onload = function () {
// do something to response
console.log(this.responseText);
};
xhttp.send('param1=param1value¶m2=param2value¶m3=param3value');
}
}
// Usage
soQuery('GET', 'https://www.example.com/query.php');
Note that the URL should be on the same domain as the function caller.
Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.
Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.
Use SpeedFactor to track your website. Itβs simple and reliable.
See how real people experience the speed of your website. Then find (and fix) your web performance problems.
Get Started