search
HomeWeChat AppletMini Program DevelopmentDetailed explanation of WeChat applet request network request operation example

The mini program provides many APIs, which greatly facilitates developers. Among them, the network request API is wx.request(object), which is the one that implements data interaction between the mini program and the developer's server. Very important API. This article mainly introduces the WeChat applet's use of the request network request operation, and analyzes the specific usage skills of the wx.request(object) network request operation in the form of examples. Friends in need can refer to it. I hope it can help everyone.

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)
The simplest usage is as follows (take POST request as Example)


bindSearchChange:function(e){
 var keyword = e.detail.value;
 wx.request({
 url:'xxxxxxxxx',
 data:{},
 header: {'Content-Type': 'application/json'},
 success: function(res) {
 console.log(res)
 }
 })
}

Below we write the request in the http.js file under the service file, the code is as follows


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 so that it can be used in other files. Since the js function is executed asynchronously, the return function is the callback function, not the specific data

In order to facilitate other files to call this method, we register it as a global function in the app.js file in the root directory, as follows


//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
 }
})

At this time, this req It is global. When calling, we can use

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)
 });
 }
})

WeChat applet provides Many APIs, including network, media, data, etc., also provide many components, making it very convenient to develop small programs.

Related recommendations:

Summary of functions of WeChat mini program

Tutorial on implementing the function of adding mobile phone contacts to WeChat mini program

How to implement the WeChat applet display drop-down list function

The above is the detailed content of Detailed explanation of WeChat applet request network request operation example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor