UtilsHttp.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Created by MSIK on 2018/12/22.
  3. */
  4. window.userData = {
  5. httpData : {
  6. data: {
  7. avatarUrl: "/media/img/oGxOa5SQ1ltBWWsVRirUvXUBGRRY.png",
  8. city: "Harbin",
  9. gender: 1,
  10. nickName: "汪汪Husky",
  11. openid: "oGxOa5SQ1ltBWWsVRirUvXUBGRRY",
  12. province: "Heilongjiang"
  13. },
  14. },
  15. // oGxOa5bd61v9jI_indIdQtI2T5P8 大佬
  16. // oGxOa5bjKLaCSbkPev09yVze0WSA 晓明
  17. // oGxOa5del9BkzJ3e8fLVzI4mlvEI 小狐仙
  18. // oGxOa5eZUuWCNCoEUQIjtu2uN9BM 9527
  19. // oGxOa5SQ1ltBWWsVRirUvXUBGRRY 汪汪
  20. openId: "oGxOa5bjKLaCSbkPev09yVze0WSA"
  21. }
  22. window.httpUtils = {
  23. http : "https://www.yuyekeji.cn/",
  24. setUserData : "https://www.yuyekeji.cn/Iamrichman_app/setUserData/",
  25. // setUserData : "https://www.youaigclm.top/Iamrichman_app/setUserData/",
  26. // getUserData : "https://www.youaigclm.top/api/getJson",
  27. getUserData : "https://www.yuyekeji.cn/Iamrichman_app/getUserData/",
  28. // getUserData : "https://www.youaigclm.top/Iamrichman_app/getUserData/",
  29. PlayButton : "https://www.youaigclm.top/media/PlayButtonnew.png",
  30. Share : "https://www.youaigclm.top/media/Share.png",
  31. Get: function(url,reqData,callbackSuccess,callbackFail){
  32. var self = this;
  33. url += "?";
  34. for(var item in reqData){
  35. url += item +"=" +reqData[item] +"&";
  36. }
  37. // console.log(self.ip + url)
  38. var xhr = new XMLHttpRequest();
  39. xhr.onreadystatechange = function () {
  40. if (xhr.readyState == 4){
  41. if(xhr.status >= 200 && xhr.status < 400){
  42. var response = xhr.responseText;
  43. // console.log(response)
  44. if(response){
  45. var responseJson = JSON.parse(response);
  46. callbackSuccess(responseJson);
  47. }else{
  48. // console.log("返回数据不存在")
  49. callbackFail();
  50. }
  51. }else{
  52. // console.log("请求失败")
  53. callbackFail();
  54. }
  55. }
  56. };
  57. xhr.open("GET", url, true);
  58. xhr.send();
  59. },
  60. /**
  61. * var data = [];
  62. * data["img"] = httpImag;
  63. * @param url
  64. * @param reqData
  65. * @param callbackSuccess
  66. * @param callbackFail
  67. * @constructor
  68. */
  69. Post: function (url, reqData, callbackSuccess,callbackFail) {
  70. var self = this;
  71. // console.log(url)
  72. // console.log(reqData)
  73. //1.拼接请求参数
  74. var param = "";
  75. for(var item in reqData){
  76. param += item + "=" + reqData[item] + "&";
  77. }
  78. //2.发起请求
  79. var xhr = new XMLHttpRequest();
  80. xhr.onreadystatechange = function () {
  81. if (xhr.readyState == 4){
  82. if(xhr.status >= 200 && xhr.status < 400){
  83. var response = xhr.responseText;
  84. // console.log(response)
  85. if(response){
  86. var responseJson = JSON.parse(response);
  87. callbackSuccess(responseJson);
  88. }else{
  89. // console.log("返回数据不存在")
  90. callbackFail();
  91. }
  92. }else{
  93. // console.log("请求失败")
  94. callbackFail(false);
  95. }
  96. }
  97. };
  98. xhr.open("POST", url, true);
  99. xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
  100. xhr.send(param);//reqData为字符串形式: "key=value"
  101. },
  102. };