网站建设资讯

NEWS

网站建设资讯

cocoscreatorhttp工具类(xmlRequest)-创新互联

var httpUtils = cc.Class({
extends: cc.Component,

创新互联是一家专注于网站设计制作、成都做网站与策划设计,武汉网站建设哪家好?创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:武汉等地区。武汉做网站价格咨询:028-86922220
properties: {
    // foo: {
    //    default: null,      // The default value will be used only when the component attaching
    //                           to a node for the first time
    //    url: cc.Texture2D,  // optional, default is typeof default
    //    serializable: true, // optional, default is true
    //    visible: true,      // optional, default is true
    //    displayName: 'Foo', // optional
    //    readonly: false,    // optional, default is false
    // },
    // ...
},

statics: {
    instance: null
},

// use this for initialization
onLoad: function () {
},

httpGets: function (url, callback) {
    var xhr = cc.loader.getXMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
            var respone = xhr.responseText;
            callback(respone);
        }
    };
    xhr.open("GET", url, true);
    if (cc.sys.isNative) {
        xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
    }

    // note: In Internet Explorer, the timeout property may be set only after calling the open()
    // method and before calling the send() method.
    xhr.timeout = 5000;// 5 seconds for timeout

    xhr.send();
},

httpPost: function (url, params, callback) {
    var xhr = cc.loader.getXMLHttpRequest();
    xhr.onreadystatechange = function () {
        cc.log('xhr.readyState='+xhr.readyState+'  xhr.status='+xhr.status);
        if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
            var respone = xhr.responseText;
            callback(respone);
        }else{
              callback(-1);
        }
    };
    xhr.open("POST", url, true);
    if (cc.sys.isNative) {
        xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
    }

    // note: In Internet Explorer, the timeout property may be set only after calling the open()
    // method and before calling the send() method.
    xhr.timeout = 5000;// 5 seconds for timeout

    xhr.send(params);
}

});

httpUtils.getInstance = function () {
if (httpUtils.instance == null) {
httpUtils.instance = new httpUtils();
}
return httpUtils.instance;
};

js 单利的使用方法:

httpUtils.getInstance().httpPost('http://192.168.1.107:8081/index.php?s=/app/verify/sendverify.html', params, function (data) {
if (data === -1) {
cc.log('请检查网络!');
} else {
var jsonD = JSON.parse(data);
if (jsonD['status'] === 1) {
cc.log('发送成功');
} else {
cc.log('发送失败' + ':' + eval("'" + jsonD['message'] + "'"));

}
        }
    });

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:cocoscreatorhttp工具类(xmlRequest)-创新互联
URL地址:http://cdweb.net/article/jphsg.html