import { _decorator, Component, Node } from 'cc'; import { gameManager } from './gameManager'; import { uiManager } from '../framework/uiManager'; const { ccclass, property } = _decorator; @ccclass('HttpHelp') export class HttpHelp { /** * 请求地址的域名配置 */ static HttpsUrlTest: string = "http://testapi.mstardance.com/api/"; /** * 请求地址的域名配置 */ static HttpsUrlRelpse: string = "http://api.mstardance.com/api/"; //屏蔽掉,正式的也是测试//"http://star.mstardance.com/api/"; static httpGet(url: string, callback: Function, token: string = null!, bind: any = null) { url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url; console.log('请求地址是:', url); let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status == 200) { let respone = xhr.responseText; try { if (bind != null) { callback.call(bind, respone); } else { callback(respone); } } catch (error3) { } } else { // console.error(xhr.readyState+"httpGet 失败:"+xhr.status); } }; xhr.withCredentials = true; xhr.timeout = 5000;// 5 seconds for timeout xhr.open('GET', url, true); xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST'); xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization'); xhr.setRequestHeader("Content-Type", "application/json"); if (token && token.length > 3) { xhr.setRequestHeader("accessToken", token); } xhr.send(); } /** * post请求 * @param {string} url * @param {object} params * @param {function} callback */ static httpPost(url: string, params: any, callback: Function, token: string = null!, bind: any = null) { url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url; let xhr = new XMLHttpRequest(); // try{ xhr.onreadystatechange = function () { // console.log('xhr.readyState=' + xhr.readyState + ' xhr.status=' + xhr.status); if (xhr.readyState === 4) { let respone = xhr.responseText; if (xhr.status == 200) { try { if (bind != null) { callback.call(bind, respone); } else { callback(respone); } } catch (error2) { } } else { try { if (bind != null) { callback.call(bind, "-1"); } else { callback("-1"); } } catch (error1) { } } } }; xhr.timeout = 5000;// 5 seconds for timeout xhr.open('POST', url, true); xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST'); // xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type'); xhr.setRequestHeader("Content-type", "application/json"); if (token && token.length > 3) { xhr.setRequestHeader("accessToken", token); } xhr.send(params); // } catch (error1) { // uiManager.instance.showTips('网络异常,请稍微再试!data==-2'); // } } }