首页 > Web开发 > 详细

cocosCreator Http请求工具类

时间:2020-03-19 15:25:12      阅读:195      评论:0      收藏:0      [点我收藏+]
const HttpHelper = cc.Class({
    extends: cc.Component,

    /**
     * get请求
     * @param {string} url 
     * @param {function} callback 
     */
    get(url, callback) {
        var xhr = cc.loader.getXMLHttpRequest();
        console.log("Status: Send Get Request to " + url);
        xhr.open("GET", url, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
                callback(true,xhr.responseText); 
            } 
        };
        xhr.send();
    },

    /**
     * post请求
     * @param {string} url 
     * @param {object} params 
     * @param {function} callback 
     */
    post(url, params, callback) {
        var nums = arguments.length  
        if(nums == 2){  
            callback = arguments[1];  
            params = "";  
        }  
        var xhr = cc.loader.getXMLHttpRequest();  
        xhr.open("POST", url);  
        xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8");  
        xhr.onreadystatechange = function () {  
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
                callback(true,xhr.responseText); 
            }
        };  
        xhr.send(params); 
    }
     // update (dt) {},
});

window.HttpHelper = new HttpHelper();

cocosCreator Http请求工具类

原文:https://www.cnblogs.com/jianxiaopo/p/12524095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!