HttpHelp.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { _decorator, Component, Node } from 'cc';
  2. import { gameManager } from './gameManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('HttpHelp')
  5. export class HttpHelp {
  6. /**
  7. * 请求地址的域名配置
  8. */
  9. static HttpsUrlTest: string = "http://testapi.mstardance.com/api/";
  10. /**
  11. * 请求地址的域名配置
  12. */
  13. static HttpsUrlRelpse: string = "http://testapi.mstardance.com/api/"; //屏蔽掉,正式的也是测试//"http://star.mstardance.com/api/";
  14. static httpGet(url: string, callback: Function, token: string = null!, bind: any = null) {
  15. url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
  16. console.log('请求地址是:', url);
  17. let xhr = new XMLHttpRequest();
  18. xhr.onreadystatechange = function () {
  19. if (xhr.readyState === 4 && xhr.status == 200) {
  20. let respone = xhr.responseText;
  21. if (bind != null) {
  22. callback.call(bind, respone);
  23. } else {
  24. callback(respone);
  25. }
  26. } else {
  27. // console.error(xhr.readyState+"httpGet 失败:"+xhr.status);
  28. }
  29. };
  30. xhr.withCredentials = true;
  31. xhr.timeout = 5000;// 5 seconds for timeout
  32. xhr.open('GET', url, true);
  33. xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
  34. xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
  35. xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
  36. xhr.setRequestHeader("Content-Type", "application/json");
  37. if (token && token.length > 3) {
  38. xhr.setRequestHeader("accessToken", token);
  39. }
  40. xhr.send();
  41. }
  42. /**
  43. * post请求
  44. * @param {string} url
  45. * @param {object} params
  46. * @param {function} callback
  47. */
  48. static httpPost(url: string, params: any, callback: Function, token: string = null!, bind: any = null) {
  49. url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
  50. let xhr = new XMLHttpRequest();
  51. xhr.onreadystatechange = function () {
  52. // console.log('xhr.readyState=' + xhr.readyState + ' xhr.status=' + xhr.status);
  53. if (xhr.readyState === 4) {
  54. let respone = xhr.responseText;
  55. if (xhr.status == 200) {
  56. if (bind != null) {
  57. callback.call(bind, respone);
  58. } else {
  59. callback(respone);
  60. }
  61. } else {
  62. if (bind != null) {
  63. callback.call(bind, "-1");
  64. } else {
  65. callback("-1");
  66. }
  67. }
  68. }
  69. };
  70. xhr.timeout = 5000;// 5 seconds for timeout
  71. xhr.open('POST', url, true);
  72. xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
  73. xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST');
  74. // xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type');
  75. xhr.setRequestHeader("Content-type", "application/json");
  76. if (token && token.length > 3) {
  77. xhr.setRequestHeader("accessToken", token);
  78. }
  79. xhr.send(params);
  80. }
  81. }