axios时是比较流行的AJAX封装库之一,用于很方便地实现ajax请求的发送
从浏览器发出 XMLHttpRequests请求
从 node.js 发出 http 请求
创建AJAX
function myAjax1(obj) {
// 创建一个请求对象 xmlhttp
var xmlHttp
}
if (type.toUpperCase() == "GET") {
xmlHttp.open(obj.type, obj.url + "?" + obj.data, obj.async)
xmlHttp.send()
} else if (obj.type.toUpperCase() == "POST") {
xmlHttp.open(obj.type, obj.url, obj.async)
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlHttp.send(obj.data) //post请求通过send()将参数传递到服务器端
}
//事件处理
xmlHttp.onreadystatechange = function() {
if (xmlHttp.status == 200 && xmlHttp.readyState == 4) {
obj.success(xmlhttp.responseText)
}
}function createXmlHttp() {
if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
} catch (error) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
} else if (window.XMLHttpRequest) {
try {
xmlHttp = new XMLHttpRequest
} catch (error) {
xmlHttp = false
}
}
}