赞
踩
- import { Component, loader } from "cc";
-
-
- const url_http = "https://xqapi.haoyingsheng.top/api";
-
- export default class HttpUtil {
- /**
-
- * 请求协议的方法
- * @param path 请求接口的路径
- * @param params 参数
- * @param callBack 回调函数
- */
- public static get(path, params, callBack) {
-
- var requestUrl = url_http + path;
-
- var xhr = loader.getXMLHttpRequest();
-
- // var data=self.paramData(params);
-
- var data = params;
-
- let param = '?';
-
- for (var key in data) {
-
- var paramStr = key + "=" + data[key];
-
- if (param == "") {
-
- param += paramStr;
-
- } else {
-
- param += "&" + paramStr;
-
- }
-
- }
-
- xhr.open("GET", requestUrl + param);
-
- xhr.timeout = 5000;//
-
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
-
- xhr.onreadystatechange = function () {
-
- if (xhr.readyState === 4 && xhr.status == 200) {
-
- var respone = xhr.responseText;
-
- console.log('响应参数')
-
- console.log(respone)
-
- callBack(JSON.parse(respone));
-
- }
-
- };
-
- xhr.send();
-
- }
-
- public static POST(url, param: object = {}, callback) {
- url = url_http+url;
- var xhr = loader.getXMLHttpRequest();
- let dataStr = '';
- Object.keys(param).forEach(key => {
- dataStr += key + '=' + encodeURIComponent(param[key]) + '&';
- })
- if (dataStr !== '') {
- dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
- }
- xhr.open("POST", url, true);
- // 第一种表单提交
- // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- // 第二种 json提交
- xhr.setRequestHeader("Content-Type", "application/JSON");
-
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- let response = xhr.responseText;
- if (xhr.status >= 200 && xhr.status < 300) {
- let httpStatus = xhr.statusText;
- // callback(true, JSON.parse(response));
- callback(true, response);
-
- } else {
- callback(false, response);
- }
- }
- };
- // 第一种表单提交 和上面配合使用
- // xhr.send(JSON.stringify(dataStr));
- // 第二种 json提交 和上面配合使用
- xhr.send(JSON.stringify(param));
- }
-
- }
-
- function ccclass(arg0: string) {
- throw new Error("Function not implemented.");
- }
-
自己做的小游戏感兴趣可以扫码体验,可以交流游戏
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。