HttpUtils.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {_decorator, Component, Node} from 'cc';
  2. const {ccclass, property} = _decorator;
  3. @ccclass('HttpUtils')
  4. export class HttpUtils extends Component {
  5. // public static ip: string = "http://192.168.1.22:8201/analysis"//本地
  6. // public static ip: string = "http://yaorongtest.yaorongmedical.com/analysis"//测试环境
  7. public static ip: string = "https://yaorong.yaorongmedical.com/analysis"//正式环境
  8. // public static port: number = 8088
  9. public static async sendHttpReq(url: string) {
  10. try {
  11. const resp = await fetch("http://192.168.1.22:8088/test/aaa")
  12. // const resp = await fetch(this.ip + ":" + this.port + "/test/aaa")
  13. if (!resp.ok) {
  14. // console.log("error...");
  15. return
  16. }
  17. const data = await resp.text()
  18. // console.log("aaaaaaaaaaa= " + data);
  19. return data
  20. } catch (error) {
  21. console.log("失败= " + error);
  22. }
  23. }
  24. public static async sendPost(url: string, postData: any) {
  25. console.log("ip= " + this.ip);
  26. try {
  27. const response = await fetch(url, {
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. // 可以添加其他头部,如认证令牌
  32. // 'Authorization': 'Bearer your_token_here'
  33. },
  34. body: JSON.stringify(postData)
  35. });
  36. if (!response.ok) {
  37. // throw new Error(`HTTP error! status: ${response.status}`);
  38. console.log(`HTTP error! status: ${response.status}`);
  39. }
  40. return await response.json();
  41. } catch (error) {
  42. console.log('POST请求失败:', error);
  43. // throw error;
  44. }
  45. }
  46. }