cocos creator http请求

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    httpPost(url,params) {
        return new Promise((resolve,reject)=>{
            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;
                    resolve(respone);
                }
            };
            var url_temp = "https://xcx.52zzyx.com/" + url;
            xhr.open("POST", url_temp, true);
           
    
            // 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.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
            xhr.send(params);
        })
        
    }


    // update (dt) {}
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 滚滚蜻蜓,落在我黑铁的皮肤上 真是烦恼 那里常年流满芒果酒的甜辣 而总是爱撞击我胸膛的 却是摩托车活塞样子的心脏 ...
    南的哲尾猫阅读 256评论 0 3
  • 一、概念:数据: data数据库: DB数据库管理系统:DBMS数据库系统:DBSMySQL:数据库mysql:客...
    李景磊阅读 375评论 0 0
  • 如果生命走到尽头 如果生命走到了尽头,我是否甘心死亡 我的理想还未完成,我还要创造梦想 我还未将父母恩情回报 我还...
    带着伙伴去旅行阅读 2,596评论 0 0