前端之家收集整理的这篇文章主要介绍了
Cocos Creator网络请求,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var appScript = {
ip: "https://game.zuiqiangyingyu.net/wb",version: "1.0.0",Get: function(url,reqData,callback){
var self = this;
url += "?";
for(var item in reqData){
url += item +"=" +reqData[item] +"&";
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4){
if(xhr.status >= 200 && xhr.status < 400){
var response = xhr.responseText;
if(response){
var responseJson = JSON.parse(response);
callback(responseJson);
}else{
console.log("返回数据不存在")
callback(false);
}
}else{
console.log("请求失败")
callback(false);
}
}
};
xhr.open("GET",self.ip + url,true);
xhr.send();
},Post: function (url,callback) {
var self = this;
var param = "";
for(var item in reqData){
param += item + "=" + reqData[item] + "&";
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4){
if(xhr.status >= 200 && xhr.status < 400){
var response = xhr.responseText;
if(response){
var responseJson = JSON.parse(response);
callback(responseJson);
}else{
console.log("返回数据不存在")
callback(false);
}
}else{
console.log("请求失败")
callback(false);
}
}
};
xhr.open("POST",true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(param);
},};
module.exports = appScript;