request.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. graceUI-JS - 网络请求工具
  3. link : graceui.hcoder.net
  4. author : 5213606@qq.com 深海
  5. 版权声明 :
  6. GraceUI 的版权约束是不能转售或者将 GraceUI 直接发布到公开渠道!
  7. 侵权必究,请遵守版权约定!
  8. */
  9. module.exports = {
  10. get : function(url, data, callback, headers){
  11. uni.request({
  12. url : url,
  13. data : data,
  14. method : "GET",
  15. dataType : "json",
  16. header : headers,
  17. success : (res) => {callback(res.data);},
  18. fail : () => {uni.showToast({title:"网络请求失败", icon:"none"});}
  19. });
  20. },
  21. post : function(url, data, contentType, headers, callback){
  22. switch(contentType){
  23. case "form" :
  24. var headerObj = {'content-type' : 'application/x-www-form-urlencoded'};
  25. break;
  26. case "json" :
  27. var headerObj = {'content-type' : 'application/json'};
  28. break;
  29. default :
  30. var headerObj = {'content-type' : 'application/json'};
  31. }
  32. for(var k in headers){headerObj[k] = headers[k];}
  33. uni.request({
  34. url : url,
  35. data : data,
  36. method : "POST",
  37. dataType : "json",
  38. header : headerObj,
  39. success : (res) => {callback(res.data);},
  40. fail : () => {uni.showToast({title:"网络请求失败", icon:"none"});}
  41. });
  42. }
  43. }