自己收集總結(jié)的一些AJAX的數(shù)據(jù)請(qǐng)求抵皱;
1侦副、原生JS的AJAX請(qǐng)求
var XHR=null;
if (window.XMLHttpRequest) {
? ?// 非IE內(nèi)核
XHR = new XMLHttpRequest();
} else if (window.ActiveXObject) {
? // IE內(nèi)核,這里早期IE的版本寫法不同,具體可以查詢下
XHR = new ActiveXObject("Microsoft.XMLHTTP");
} else {
XHR = null;
}
if(XHR){
XHR.open("GET", "url");
XHR.onreadystatechange = function () {
// readyState值說明
// 0,初始化,XHR對(duì)象已經(jīng)創(chuàng)建,還未執(zhí)行open
// 1,載入,已經(jīng)調(diào)用open方法,但是還沒發(fā)送請(qǐng)求
// 2,載入完成,請(qǐng)求已經(jīng)發(fā)送完成
// 3,交互,可以接收到部分?jǐn)?shù)據(jù)
// status值說明
// 200:成功
// 404:沒有發(fā)現(xiàn)文件师崎、查詢或URl
// 500:服務(wù)器產(chǎn)生內(nèi)部錯(cuò)誤
if (XHR.readyState == 4 && XHR.status == 200) {
console.log(XHR.responseText);
}
};
XHR.send();
}
2、JQ的AJAX請(qǐng)求
$("#tijiao").click(function(){
$.ajax({
type:"GET",
url:"http://localhost/AJAX/test.php?name="+$("#name").val()+"&phone="+
$("#phone").val(),
dataType:"json",
success:function(data){
$("#success").html(data.msg);
},
error:function(){
alert("錯(cuò)誤E窃俊=耘隆!");
}
})
})
3值戳、VUE框架下的請(qǐng)求
this.$http.get('http://route.showapi.com/341-1', {
params: {
showapi_appid: 26444,
showapi_sign: 'e6ed68d43d734b78892a649fedd90cbe'
}
}).then(function(res) {
// console.log(res);
var data = res.data;
if (data && data.showapi_res_code === 0) {
that.list = data.showapi_res_body.contentlist
} else {
that.errormsg = data.showapi_res_error;
}
}, function(error) {
that.errormsg = '網(wǎng)絡(luò)異常议谷,請(qǐng)稍候重試!6楹纭卧晓!'
})
4、react框架下的請(qǐng)求
var ?id=this.props.match.params.id;
$.get('http://localhost:8080/details?id='+id, function(res) {
var arr=JSON.parse(res);?
})