ajax封装
<script>
ajax({
'methods':'post',
'url':'wenben.txt',
'type':'application/x-www-form-urlencoded;utf-8',
'data':'name=ujiuye',
'success':function (res) {
console.log(res)
}
})
function ajax(json){
var ajax = new XMLHttpRequest()
ajax.open(json.methods,json.url)
if(json.methods == 'post'){
ajax.setRequestHeader('Content-type',json.type)
ajax.send(json.data)
}
if(json.methods == 'get'){
ajax.send()
}
ajax.onreadystatechange = function(){
if(ajax.readyState ==4 ){
if(ajax.status == 200){
json.success(ajax.response)
}
}
}
}
// 字符串转json JSON.parse(res)
// 对象转字符串 JSON.stringify(res)
</script>