Http.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Global from "./Global";
  2. let URL = "https://www.19960909.com";
  3. // 39.98.43.215:9000;
  4. // let URL = "http://192.168.3.137:9001"
  5. export default class Http {
  6. // let origin:number = 0;
  7. // static sessionId:number;
  8. // static userId:number;
  9. // static master_url:number;
  10. static url:string = URL;
  11. static sendRequest(path:string,data:object,handler:Function,extraUrl?:string) {
  12. return;
  13. let xhr = new XMLHttpRequest()
  14. xhr.timeout = 5000;
  15. var str = "?msg=" + JSON.stringify(data);
  16. // for(var k in data){
  17. // if(str != "?"){
  18. // str += "&";
  19. // }
  20. // str += k + "=" + data[k];
  21. // }
  22. if(extraUrl == null){
  23. extraUrl = Http.url;
  24. }
  25. var requestURL = extraUrl + path + encodeURI(str);
  26. console.log("RequestURL:" + requestURL);
  27. xhr.open("GET",requestURL, true);
  28. if (cc.sys.isNative){
  29. xhr.setRequestHeader("Accept-Encoding","gzip,deflate");
  30. xhr.setRequestHeader("contentType","text/html;charset=UTF-8" );
  31. }
  32. xhr.onreadystatechange = function() {
  33. if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
  34. Global.instance.NetStatus = true;
  35. //console.log("http res("+ xhr.responseText.length + "):" + xhr.responseText);
  36. try {
  37. console.log("------------------1");
  38. var ret = JSON.parse(xhr.responseText);
  39. if(handler !== null){
  40. handler(ret);
  41. } /* code */
  42. } catch (e) {
  43. console.log("err:" + e);
  44. //handler(null);
  45. }
  46. finally{
  47. // if(cc.vv && cc.vv.wc){
  48. // // cc.vv.wc.hide();
  49. // }
  50. }
  51. }
  52. else{
  53. //Global.instance.NetStatus = false;
  54. }
  55. };
  56. // if(cc.vv && cc.vv.wc){
  57. // //cc.vv.wc.show();
  58. // }
  59. try {
  60. xhr.send();
  61. } catch (error) {
  62. Global.instance.NetStatus = false;
  63. //console.log(" Global.instance.NetStatus "+Global.instance.NetStatus);
  64. console.log("HTTP "+error);
  65. }
  66. return xhr;
  67. };
  68. static sendPost(path:string,data:object,handler:Function,extraUrl?:string) {
  69. return;
  70. let xhr = new XMLHttpRequest()
  71. xhr.timeout = 5000;
  72. xhr.open("POST", Http.url + path, true);
  73. // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");//缺少这句,后台无法获取参数
  74. xhr.setRequestHeader("Content-type", "application/json");
  75. xhr.onreadystatechange = function() {
  76. //console.info(xhr.getResponseHeader('content-type')==='application/json');
  77. if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
  78. Global.instance.NetStatus = true;
  79. //console.log("http res("+ xhr.responseText.length + "):" + xhr.responseText);
  80. try {
  81. console.log("------------------2");
  82. var ret = JSON.parse(xhr.responseText);
  83. if(handler !== null){
  84. handler(ret);
  85. } /* code */
  86. } catch (e) {
  87. console.log("err:" + e);
  88. //handler(null);
  89. }
  90. finally{
  91. // if(cc.vv && cc.vv.wc){
  92. // // cc.vv.wc.hide();
  93. // }
  94. }
  95. }
  96. else{
  97. }
  98. };
  99. //console.info(JSON.stringify(data));
  100. try {
  101. xhr.send("a=1&a=2");
  102. } catch (error) {
  103. Global.instance.NetStatus = false;
  104. }
  105. return xhr;
  106. };
  107. // 中文乱码解决
  108. // if (request.getMethod().equalsIgnoreCase("POST"))
  109. // {
  110. // request.setCharacterEncoding("UTF-8");
  111. // System.out.println(request.getParameter("value"));
  112. // }
  113. // // 处理GET请求
  114. // else if (request.getMethod().equalsIgnoreCase("GET"))
  115. // {
  116. // String tmp = request.getParameter("value");
  117. // String a = new String(tmp.getBytes("ISO-8859-1") , "UTF-8");
  118. // System.out.println(a);
  119. // }
  120. }