在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、新建一个api目录,与pages同级 2、在api目录下新建一个api.js文件 3、编写代码 const host = 'http://test.test.cn' const wxRequest = function (params, url) { wx.showToast({ title: '加载中...', icon: 'loading' }) wx.request({ url: url, method: params.method || 'GET', data: params.data || {}, header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, success: function (res) { params.success && params.success(res) wx.hideToast() }, fail: function (res) { params.fail && params.fail(res) }, complete: function (res) { params.complete && params.complete(res) } }) } const bindCode = function (params) { wxRequest(params, host + '/AppPhone/Api') } const startScan = function (params) { wxRequest(params, host + '/AppPhone/Api') } const imgToservers = function (params) { wxRequest(params, host + '/AppPhone/Api/upload_files') } module.exports = { bindCode, startScan, imgToservers } 4、在js中调用 import api from '../../api/api.js' Page({ /** * 页面的初始数据 */ data: { shopCode: '', deviceId: '', }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var getShopInfo = wx.getStorageSync('shopInfo'); if (getShopInfo) { this.setData({ shopCode: getShopInfo.shop_code, deviceId: getShopInfo.device_id }) }else{ wx.redirectTo({ url: '../bindnum/bindnum?type=1' }) } }, beginScan: function(){ var _that = this; api.startScan({ method: 'POST', data: { shop_code: this.data.shopCode, device_id: this.data.deviceId, method: 'get_phone_scanner_id' }, success: function(res){ console.log(res.data); } }); }, }) 5、问题 一开始wx.request中header设置为 header: { 'content-type': 'application/json' // 默认值 },
header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, 就可以成功调用了
参考文章: wx.request发送与服务端接受
|
请发表评论