UtilsHttp.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Created by MSIK on 2018/12/22.
  3. */
  4. window.httpUtils = {
  5. http : "https://www.yuyekeji.cn/",
  6. // setUserData : "https://www.yuyekeji.cn/Iamrichman_app/setUserData/",
  7. setUserData : "https://www.youaigclm.top/Iamrichman_app/setUserData/",
  8. // getUserData : "https://www.youaigclm.top/api/getJson",
  9. // getUserData : "https://www.yuyekeji.cn/Iamrichman_app/getUserData/",
  10. getUserData : "https://www.youaigclm.top/Iamrichman_app/getUserData/",
  11. PlayButton : "https://www.youaigclm.top/media/PlayButton.png",
  12. Share : "https://www.youaigclm.top/media/Share.png",
  13. // extends: cc.Component,
  14. //
  15. // properties: {
  16. // // foo: {
  17. // // default: null, // The default value will be used only when the component attaching
  18. // // to a node for the first time
  19. // // url: cc.Texture2D, // optional, default is typeof default
  20. // // serializable: true, // optional, default is true
  21. // // visible: true, // optional, default is true
  22. // // displayName: 'Foo', // optional
  23. // // readonly: false, // optional, default is false
  24. // // },
  25. // // ...
  26. // },
  27. //
  28. // statics: {
  29. // instance: null
  30. // },
  31. // use this for initialization
  32. onLoad: function () {
  33. },
  34. // get: function (url, callbackSuccess,callbackFail) {
  35. // var xhr = cc.loader.getXMLHttpRequest();
  36. //
  37. // cc.log("调用了吗 请求");
  38. // xhr.onreadystatechange = function () {
  39. // if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  40. // var respone = xhr.responseText;
  41. // callbackSuccess(respone);
  42. // }else{
  43. // callbackFail();
  44. // }
  45. // }.bind(this);
  46. // xhr.open("GET", url, true);
  47. // if (cc.sys.isNative) {
  48. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  49. // }
  50. //
  51. // // note: In Internet Explorer, the timeout property may be set only after calling the open()
  52. // // method and before calling the send() method.
  53. // xhr.timeout = 5000;// 5 seconds for timeout
  54. //
  55. // xhr.send();
  56. // },
  57. Get: function(url,reqData,callbackSuccess,callbackFail){
  58. var self = this;
  59. url += "?";
  60. for(var item in reqData){
  61. url += item +"=" +reqData[item] +"&";
  62. }
  63. // console.log(self.ip + url)
  64. var xhr = new XMLHttpRequest();
  65. xhr.onreadystatechange = function () {
  66. if (xhr.readyState == 4){
  67. if(xhr.status >= 200 && xhr.status < 400){
  68. var response = xhr.responseText;
  69. // console.log(response)
  70. if(response){
  71. var responseJson = JSON.parse(response);
  72. callbackSuccess(responseJson);
  73. }else{
  74. // console.log("返回数据不存在")
  75. callbackFail();
  76. }
  77. }else{
  78. // console.log("请求失败")
  79. callbackFail();
  80. }
  81. }
  82. };
  83. xhr.open("GET", url, true);
  84. xhr.send();
  85. },
  86. /**
  87. * var data = [];
  88. * data["img"] = httpImag;
  89. * @param url
  90. * @param reqData
  91. * @param callbackSuccess
  92. * @param callbackFail
  93. * @constructor
  94. */
  95. Post: function (url, reqData, callbackSuccess,callbackFail) {
  96. var self = this;
  97. // console.log(url)
  98. // console.log(reqData)
  99. //1.拼接请求参数
  100. var param = "";
  101. for(var item in reqData){
  102. param += item + "=" + reqData[item] + "&";
  103. }
  104. //2.发起请求
  105. var xhr = new XMLHttpRequest();
  106. xhr.onreadystatechange = function () {
  107. if (xhr.readyState == 4){
  108. if(xhr.status >= 200 && xhr.status < 400){
  109. var response = xhr.responseText;
  110. // console.log(response)
  111. if(response){
  112. var responseJson = JSON.parse(response);
  113. callbackSuccess(responseJson);
  114. }else{
  115. // console.log("返回数据不存在")
  116. callbackFail();
  117. }
  118. }else{
  119. // console.log("请求失败")
  120. callbackFail(false);
  121. }
  122. }
  123. };
  124. xhr.open("POST", url, true);
  125. xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
  126. xhr.send(param);//reqData为字符串形式: "key=value"
  127. },
  128. //
  129. // post: function (url, params, callbackSuccess,callbackFail) {
  130. // var xhr = cc.loader.getXMLHttpRequest();
  131. // xhr.onreadystatechange = function () {
  132. // cc.log('xhr.readyState='+xhr.readyState+' xhr.status='+xhr.status);
  133. // if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  134. // var respone = xhr.responseText;
  135. // callbackSuccess(respone);
  136. // }else{
  137. // callbackFail();
  138. // }
  139. // }.bind(this);
  140. // xhr.open("POST", url, true);
  141. // if (cc.sys.isNative) {
  142. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  143. // }
  144. //
  145. // // note: In Internet Explorer, the timeout property may be set only after calling the open()
  146. // // method and before calling the send() method.
  147. // xhr.timeout = 5000;// 5 seconds for timeout
  148. //
  149. // xhr.send(params);
  150. // }
  151. };
  152. // httpUtils.getInstance = function () {
  153. // if (httpUtils.instance == null) {
  154. // httpUtils.instance = new httpUtils();
  155. // }
  156. // return httpUtils.instance;
  157. // };
  158. // module.exports = httpUtils;