Cocos Creator网络请求

前端之家收集整理的这篇文章主要介绍了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] +"&";
        }
        // 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,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",true);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
        xhr.send(param);//reqData为字符串形式: "key=value"
    },};

module.exports = appScript;
原文链接:https://www.f2er.com/cocos2dx/338173.html

猜你在找的Cocos2d-x相关文章