/*
 * @Author: cmd 
 * @wechat: cmd_develop 
 * @qq群: 790373986
 * @Date: 2019-04-23 15:33:22 
 * @Last Modified by: cmd
 * @Last Modified time: 2019-04-23 17:14:25
 * @Description: 
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class http extends cc.Component {
    async http_post(url,parme) {
        return new Promise((resolve,reject)=>{
            var xhr = cc.loader.getXMLHttpRequest();
            xhr.onreadystatechange = function () {                
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                    var respone = xhr.responseText;                    
                    resolve(respone);
                }
            };

            xhr.ontimeout = function (e) {
                // XMLHttpRequest 超时。在此做某事。
                resolve('error');
            };
            
            xhr.open("POST", url, 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 = 2000;// 5 seconds for timeout
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");      
            //let parme = 123;       
            xhr.send(parme);
        })        
    }
}
