This article mainly introduces the request network request operation used by WeChat applet, and analyzes the specific usage skills of wx.request(object) network request operation in the form of examples. Friends in need can refer to this article
The example describes the WeChat applet using the request network to request operations. Share it with everyone for your reference, the details are as follows:
The applet provides a lot of APIs, which greatly facilitates developers. Among them, the network request API is wx.request(object)
, which It is a very important API for data interaction between small programs and the developer's server.
The official parameter description is as follows
OBJECT parameter description:
##Parameter name | Type | Required | Description |
---|---|---|---|
url | String | is | Developer server interface address |
data | Object、String | No | Request parameters |
header | Object | No | Set the request header, Referer cannot be set in the header |
method | String | No | Default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT , DELETE, TRACE, CONNECT |
success | Function | No | Receive the callback function successfully returned by the developer service, res = {data: 'Content returned by the developer server'} |
fail | Function | No | Callback for failed interface call Function |
complete | Function | No | The callback function at the end of the interface call (will be executed if the call is successful or failed) |
bindSearchChange:function(e){ var keyword = e.detail.value; wx.request({ url:'xxxxxxxxx', data:{}, header: {'Content-Type': 'application/json'}, success: function(res) { console.log(res) } }) }
var rootDocment = 'hxxxxx';//你的域名 function req(url,data,cb){ wx.request({ url: rootDocment + url, data: data, method: 'post', header: {'Content-Type': 'application/json'}, success: function(res){ return typeof cb == "function" && cb(res.data) }, fail: function(){ return typeof cb == "function" && cb(false) } }) } module.exports = { req: req }
module.exports exposes the req method This method can be used in other files. Since the js function is executed asynchronously, the return function is the callback function instead of the specific data.
//app.js var http = require('service/http.js') App({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }, getUserInfo:function(cb){ var that = this if(this.globalData.userInfo){ typeof cb == "function" && cb(this.globalData.userInfo) }else{ //调用登录接口 wx.login({ success: function () { wx.getUserInfo({ success: function (res) { that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) } }) } }) } }, globalData:{ userInfo:null }, func:{ req:http.req } })
getApp.func.req() to call, The details are as follows
var app = getApp() Page({ data: { }, onLoad: function (opt) { //console.log(opt.name) app.func.req('/api/get_data',{},function(res){ console.log(res) }); } })
How to determine holidays in js
How to query weather forecast in Angular
How to display input content in Angular
The above is the detailed content of How to use the request network to perform request operations in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!