<script>
// get
function ajaxGet(url, params = '') {
var xhr = new XMLHttpRequest();
xhr.open('get', url + params);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let res = JSON.parse(xhr.responseText);
}
}
}
// post
function ajaxGet(url, query = '') {
var xhr = new XMLHttpRequest();
xhr.open('post', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(query);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let res = JSON.parse(xhr.responseText);
}
}
}
// get post
function selAjax(method, url, search) {
var xhr = new XMLHttpRequest();
if (method == 'get') {
xhr.open(method, url + search);
xhr.send();
} else if (method == 'post') {
xhr.open(method, url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(search);
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let res = JSON.parse(xhr.responseText);
}
}
}
</script>
|