top-block-game-cocos-demo/assets/typescript/ajax.ts

21 lines
628 B
TypeScript

export default {
// hosturl: "http://192.168.2.36:8080",
hosturl: "http://81.71.13.173:8080",
sendAjax(url: string, data: object, method: string, callback: Function) {
let xhr = new XMLHttpRequest();
xhr.open(method, this.hosturl + url, true);
let token = localStorage.getItem("token");
if (token) {
xhr.setRequestHeader("Authorization", token);
}
let params = JSON.stringify(data);
xhr.send(params);
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && xhr.status == 200) {
let res = JSON.parse(xhr.responseText);
callback(res);
}
};
},
};