美文网首页
cocos creator http请求

cocos creator http请求

作者: cmd_ts | 来源:发表于2018-10-25 20:15 被阅读329次
// 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) {}
}

相关文章

网友评论

      本文标题:cocos creator http请求

      本文链接:https://www.haomeiwen.com/subject/sfactqtx.html