用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,登录网站

小程序社区 首页 教程 新手教程 查看内容

微信小程序设置http请求的步骤详解----结合后台接口文档

天下雪 2017-5-13 00:08

分享者:kingrome2017,来自原文地址var root = "https://testapi.bidata.com.cn/platform/v3";var env = "dev";var _getURL = function(url) { if(env == "mock") { return url + ".json"; } else { return root + ...

分享者:kingrome2017,来自原文地址

  1. var root = "https://testapi.bidata.com.cn/platform/v3";
  2. var env = "dev";
  3. var _getURL = function(url) {
  4. if(env == "mock") {
  5. return url + ".json";
  6. } else {
  7. return root + url;
  8. }
  9. };
  10. var _request = function(url,method,data,onSuccess,onError) {
  11. data = data || {};
  12. var behavior = data._behavior;
  13. if(behavior) {
  14. delete data._behavior;
  15. } else {
  16. behavior = "";
  17. }
  18. wx.request({
  19. "url" : _getURL(url),
  20. "method" : method,
  21. "data" : data,
  22. "success" : function(result) {
  23. var code = result.data.CODE || result.data.code;
  24. if(code == 200) {
  25. onSuccess(result);
  26. } else if(onError && typeof onError == 'function'){
  27. onError(result);
  28. } else {
  29. // if(result.data.code == 401) {
  30. // App.fireEvent("evt_invalid_token",result);
  31. // }
  32. // wx.showModal({
  33. // "title" : "提示",
  34. // "content" : result.data.msg,
  35. // "showCancel":false
  36. // });
  37. }
  38. },
  39. "error" : onError || function(e) {
  40. console.error(e);
  41. },
  42. "header" : {
  43. // "X-Token" : wx.getStorageSync("token"),
  44. // "X-Behavior" : behavior,
  45. "X-Uid":"LMKCVXSU",
  46. "X-Nonce":"45494415",
  47. "X-Timestamp":"1493689633656",
  48. "X-Signature":'96f0ca5db1564eabd17c844d9927738fb9fed3a6'
  49. }
  50. }
  51. )};
  52. module.exports={
  53. $get : function(url,data,onSuccess,onError) {
  54. _request(url,"GET",data,onSuccess,onError);
  55. },
  56. $put : function(url,data,onSuccess,onError) {
  57. _request(url,"PUT",data,onSuccess,onError);
  58. },
  59. $post : function(url,data,onSuccess,onError) {
  60. _request(url,"POST",data,onSuccess,onError);
  61. },
  62. $delete : function(url,data,onSuccess,onError) {
  63. _request(url,"DELETE",data,onSuccess,onError);
  64. },
  65. root:root
  66. }

说明: 
调取正式上线查询接口文档 
封装http

鲜花
鲜花
鸡蛋
鸡蛋
分享至 : QQ空间
收藏