|
@@ -1,15 +1,14 @@
|
|
|
import { _decorator, Component, Node } from 'cc';
|
|
|
import { config } from './config';
|
|
|
+import { tools } from './tools';
|
|
|
const { ccclass, property } = _decorator;
|
|
|
@ccclass('http')
|
|
|
export class http {
|
|
|
- public static domain = "https://zcapi.xwrun.com";
|
|
|
-
|
|
|
static post(url,data,call_back){
|
|
|
- var xml = new XMLHttpRequest()
|
|
|
- xml.open('POST',http.domain+url)
|
|
|
- xml.setRequestHeader('Content-Type','application/json');
|
|
|
- xml.send(JSON.stringify(data));
|
|
|
+ var xml = new XMLHttpRequest()
|
|
|
+ xml.open('POST',config.domain+url)
|
|
|
+ xml.setRequestHeader('Content-Type','application/json');
|
|
|
+ xml.send(JSON.stringify(data));
|
|
|
var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
|
|
|
array.forEach(function (eventName) {
|
|
|
xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
|
|
@@ -50,13 +49,16 @@ export class http {
|
|
|
} else if (xml.readyState === 0) {
|
|
|
call_back( 'Request hasn\'t been initiated!',null);
|
|
|
console.log("status ==", 'Request hasn\'t been initiated!')
|
|
|
+ } else {
|
|
|
+ console.log('请求出错=',url)
|
|
|
+ tools.showToast('请求出错')
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
static get(url,call_back){
|
|
|
var xml = new XMLHttpRequest()
|
|
|
- xml.open('GET',http.domain+url)
|
|
|
+ xml.open('GET',config.domain+url)
|
|
|
xml.setRequestHeader('Content-Type','application/json');
|
|
|
xml.send()
|
|
|
var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
|
|
@@ -79,10 +81,13 @@ export class http {
|
|
|
|
|
|
// Special event
|
|
|
xml.onreadystatechange = function () {
|
|
|
- if (xml.readyState === 4 && xml.status >= 200) {
|
|
|
+ if(xml.readyState === 4 && xml.status >= 200) {
|
|
|
call_back(null,xml.responseText);
|
|
|
} else if (xml.status === 404) {
|
|
|
call_back('404 page not found!',null);
|
|
|
+ } else {
|
|
|
+ console.log('请求出错=',url)
|
|
|
+ tools.showToast('请求出错')
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -117,7 +122,7 @@ export class http {
|
|
|
|
|
|
public static run_get(url,call_back){
|
|
|
var xml = new XMLHttpRequest()
|
|
|
- xml.open('GET',http.domain+url)
|
|
|
+ xml.open('GET',config.domain+url)
|
|
|
xml.setRequestHeader('Content-Type','application/json');
|
|
|
xml.send();
|
|
|
|
|
@@ -135,24 +140,27 @@ export class http {
|
|
|
}else if (eventName === 'onerror') {
|
|
|
|
|
|
}
|
|
|
- console.log("str==",str)
|
|
|
- console.log("lstr==",lstr)
|
|
|
+ // console.log("str==",str)
|
|
|
+ // console.log("lstr==",lstr)
|
|
|
};
|
|
|
});
|
|
|
|
|
|
// Special event
|
|
|
xml.onreadystatechange = function () {
|
|
|
- if (xml.readyState === 4 && xml.status >= 200) {
|
|
|
- call_back(null,xml.responseText);
|
|
|
- } else if (xml.status === 404) {
|
|
|
- call_back('404 page not found!',null);
|
|
|
- }
|
|
|
+ if(xml.readyState === 4 && xml.status >= 200) {
|
|
|
+ call_back(null,xml.responseText);
|
|
|
+ } else if (xml.status === 404) {
|
|
|
+ call_back('404 page not found!',null);
|
|
|
+ } else {
|
|
|
+ console.log('请求出错=',url)
|
|
|
+ tools.showToast('请求出错')
|
|
|
+ }
|
|
|
};
|
|
|
}
|
|
|
|
|
|
public static run_post(url,data,call_back){
|
|
|
var xml = new XMLHttpRequest()
|
|
|
- xml.open('POST',http.domain+url)
|
|
|
+ xml.open('POST',config.domain+url)
|
|
|
xml.setRequestHeader('Content-Type','application/json');
|
|
|
xml.setRequestHeader('token',config.TOKEN);
|
|
|
xml.send(JSON.stringify(data));
|
|
@@ -178,11 +186,14 @@ export class http {
|
|
|
|
|
|
// Special event
|
|
|
xml.onreadystatechange = function () {
|
|
|
- if (xml.readyState === 4 && xml.status >= 200) {
|
|
|
- call_back(null,xml.responseText);
|
|
|
- } else if (xml.status === 404) {
|
|
|
- call_back('404 page not found!',null);
|
|
|
- }
|
|
|
+ if(xml.readyState === 4 && xml.status >= 200) {
|
|
|
+ call_back(null,xml.responseText);
|
|
|
+ } else if (xml.status === 404) {
|
|
|
+ call_back('404 page not found!',null);
|
|
|
+ } else {
|
|
|
+ console.log('请求出错=',url)
|
|
|
+ tools.showToast('请求出错')
|
|
|
+ }
|
|
|
};
|
|
|
}
|
|
|
}
|