| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import {_decorator, Component, Node} from 'cc';
- const {ccclass, property} = _decorator;
- @ccclass('HttpUtils')
- export class HttpUtils extends Component {
- // public static ip: string = "http://192.168.1.22:8201/analysis"//本地
- // public static ip: string = "http://yaorongtest.yaorongmedical.com/analysis"//测试环境
- public static ip: string = "https://yaorong.yaorongmedical.com/analysis"//正式环境
- // public static port: number = 8088
- public static async sendHttpReq(url: string) {
- try {
- const resp = await fetch("http://192.168.1.22:8088/test/aaa")
- // const resp = await fetch(this.ip + ":" + this.port + "/test/aaa")
- if (!resp.ok) {
- // console.log("error...");
- return
- }
- const data = await resp.text()
- // console.log("aaaaaaaaaaa= " + data);
- return data
- } catch (error) {
- console.log("失败= " + error);
- }
- }
- public static async sendPost(url: string, postData: any) {
- console.log("ip= " + this.ip);
- try {
- const response = await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- // 可以添加其他头部,如认证令牌
- // 'Authorization': 'Bearer your_token_here'
- },
- body: JSON.stringify(postData)
- });
- if (!response.ok) {
- // throw new Error(`HTTP error! status: ${response.status}`);
- console.log(`HTTP error! status: ${response.status}`);
- }
- return await response.json();
- } catch (error) {
- console.log('POST请求失败:', error);
- // throw error;
- }
- }
- }
|