您当前位置: 首页 > Cocos > 精品代码 > Cocos Creator HTTP通用请求库
Cocos Creator HTTP通用请求库
收藏 报错 好评2

Cocos Creator HTTP通用请求库

10.00 金币

源码类型:精品代码

上传日期:2018-03-27 00:12:24

源码编号:1622

高速下载源码

浏览:970 收藏:1 下载:1

详细介绍

const HTTPSERVER = "https://www.zhangyou.org";//"http://192.168.1.119";//"http://127.0.0.158";       /*通信网址*/
const JSONSERVER = HTTPSERVER+"/json.php";          /*通信主入口*/
const WXLOGIN    = HTTPSERVER+"/login/weixinopen.php"; /*微信登录接口*/
const PAYSERVER    = HTTPSERVER+"/pay.php"; /*微信登录接口*/

module.exports = {

    URL(Uid,data,handler){


        try {

            if(Uid == 'H'){

                 var Url = HTTPSERVER;
            
            }else if(Uid == 'J'){

                 var Url = JSONSERVER;
            
            }else if(Uid == 'WX'){

                 var Url = WXLOGIN;
            
            }else if(Uid == 'P'){
                
                 var Url = PAYSERVER;
                            
            }else{

                var Url = JSONSERVER;
            }
           

            var xhr =    cc.loader.getXMLHttpRequest();

            xhr.onreadystatechange = function () {

                if (xhr.readyState === 4) {

                    if(handler){

                         

                        handler(xhr.responseText,xhr.status);
                    }
                }
            };


            var DATA = null;
            var lx = 1;
            

            if(data.ac == "GET"){
                lx = 1;
                if(data.ac){

                    delete data.ac;
                }


                var str = "?";

                for(var k in data){

                    if(str != "?"){
                        str += "&";
                    }
                  

                    str += k + "=" + data[k];

                }

                Url = Url+encodeURI(str);

                xhr.open("GET", Url , true);

            }else{
                lx = 2;
                if(data.ac){
                    delete data.ac;
                }
              
                xhr.open("POST", Url);

                var str = "";
                var input = 0;
                for(var k in data){

                    if(input > 0){
                        str += "&";
                    }

                    str += k + "=" + data[k];
                    input++;
                }

                DATA = str;
            }

            if (cc.sys.isNative) {

                xhr.setRequestHeader("Accept-Encoding","gzip,deflate");
            }

            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.timeout = 10000;

            if(DATA){

                xhr.send(DATA);

            }else{

                xhr.send();
                
            }

        }catch (e) {
  
        }

    },SERVERHTPT(){


        return HTTPSERVER;

    }

}
客服 反馈