cocos-creator使用记录22_使用微信和原生js的http的get和post


1.http的get和post请求
Get: function(callback){
    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", "https://www.mysite.net/common/config/info?app_name=2048&version=1.1.0", true);
    xhr.send();
},
Post: function (reqData, callback) {
    //拼接请求参数
    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;
                // 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", "https://www.mysite.net/common/config/info/", true);
    xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");  
    xhr.send(param);//reqData为字符串形式: "key=value"
},

注意:js的http函数似乎不能复用,必须每次都重新创建一个XMLHttpRequest。

2.使用微信的http接口
httpRequest: function(url, data, call = function(){}, sync = false, method = 'POST', json = false){ //默认'POST',根据情况设置为'GET'
    if(!(cc.sys.platform === cc.sys.WECHAT_GAME)) return;
    if(json){
        wx.request({
            url: url,
            data: data,
            method: method || "POST",
            header: {'content-type': 'application/json'},
            success(res){call(res);},
            fail(){call();}
        });
    }else{
        wx.request({
            url: url,
            data: data,
            method: method || "POST",
            header: {'content-type': 'application/x-www-form-urlencoded'},
            success(res){call(res);},
            fail(){call();}
        });
    }
},

httpRequestConfig: function() { //获得后台启动配置信息
    common.httpRequest(common.url.path + common.url.info, {app_name:common.app_name, version:common.version},
        function(data){
            if(data){
                var $data = data.data.data;
                console.log("启动配置:", data)
                common.isShowGift = parseInt($data.gift);
            }
        },
    false, "GET");
},

注意:微信的http函数可以复用。

猜你喜欢

转载自blog.csdn.net/haibo19981/article/details/81215763
今日推荐