ajax获取数据
最后更新:2020-01-18 09:05
简介:
默认异步GET方式,参数为url地址,data格式:a=1&b=2&c=3
function f5iNWnPDTC(type, url) {
var ajaxData = {
async: "true",
data: null,
dataType: "text",
contentType: "application/x-www-form-urlencoded",
success: function (data) { document.getElementById('result').innerHTML=data; },
error: function (err) { },
}
var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.responseType = ajaxData.dataType;
type =type.toUpperCase();
if (type === "GET") {
url = url + "?" + ajaxData.data;
xhr.open(type, url, ajaxData.async)
xhr.send()
}
if (type === "POST") {
xhr.open(type, url, ajaxData.async)
xhr.setRequestHeader("Content-Type", ajaxData.contentType)
xhr.send(ajaxData.data)
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
ajaxData.success(xhr.responseText)
} else {
ajaxData.error(xhr.responseText)
}
}
}