当前位置 : 主页 > 手机开发 > 其它 >

Cocos Creator网络请求

来源:互联网 收集:自由互联 发布时间:2021-06-13
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] + "" ; } // console.log(self.
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] +"&";
        }
        // console.log(self.ip + url)
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4){
                if(xhr.status >= 200 && xhr.status < 400){
                    var response = xhr.responseText;
                    // console.log(response)
                    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, reqData, callback) {
        var self = this;
        // console.log(url)
        // console.log(reqData)
        //1.拼接请求参数
        var param = "";
        for(var item in reqData){
            param += item + "=" + reqData[item] + "&";
        }
        //2.发起请求
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4){
                if(xhr.status >= 200 && xhr.status < 400){
                    var response = xhr.responseText;
                    // console.log(response)
                    if(response){
                        var responseJson = JSON.parse(response);
                        callback(responseJson);
                    }else{
                        console.log("返回数据不存在")
                        callback(false);
                    }
                }else{
                    console.log("请求失败")
                    callback(false);
                }
            }
        };
        xhr.open("POST", self.ip + url, true);
        xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");  
        xhr.send(param);//reqData为字符串形式: "key=value"
    },


};

module.exports = appScript;
网友评论