cocos creator http工具类(xmlRequest)

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

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'] + "'"));

            }
        }
    });

转载于:https://blog.51cto.com/3488123/2105261

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator编辑器提供了一套强大的功能,使开发者可以轻松地编写工具来增强开发流程。以下是Cocos Creator编辑器如何编写工具的步骤: 1.熟悉Cocos Creator编辑器:首先,开发者需要熟悉Cocos Creator编辑器的基本操作和功能。这包括了场景编辑、节点操作、组件配置等。 2.了解Cocos Creator的插件机制:Cocos Creator提供了插件机制,开发者可以通过插件扩展编辑器的功能。开发者可以在官方文档中找到关于插件开发的详细说明。 3.确定工具的需求:开发者需要明确工具的需求和目标。这可以是一项新功能、自动化操作、场景批量处理等。确保明确工具的功能和预期效果。 4.开发插件:根据工具的需求,开发者可以通过插件机制来实现工具。插件可以包括自定义的编辑器窗口、面板、节点操作脚本等。 5.使用API扩展功能:Cocos Creator提供了丰富的API来扩展编辑器的功能。开发者可以使用这些API来实现工具的核心功能。 6.测试和调试:在编写工具过程中,开发者需要进行反复的测试和调试以确保工具的正确性和可靠性。这可以包括单元测试、集成测试和用户界面测试等。 7.发布和分享工具:完成工具开发后,开发者可以考虑将其发布和分享。开发者可以将工具打包成插件,供其他开发者使用和安装。 总之,使用Cocos Creator编辑器编写工具可以有效地提高开发效率和质量。通过插件机制和丰富的API,开发者可以根据自己的需求自定义和扩展编辑器的功能。希望这些步骤能对您编写Cocos Creator编辑器工具有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值