search
HomeWeChat AppletMini Program DevelopmentWeChat Mini Program Network Request Detailed Description

This article mainly introduces the detailed description of network requests for WeChat mini programs. The editor thinks it’s pretty good, so I’d like to share it with you now and give it as a reference. Let’s follow the editor and take a look.

When we talked about configuration in the previous article, we said that when developing small programs, you can choose to have APPID or without APPID. There are two ways.

1. When there is an APPID, network communication can only communicate with the specified domain name. If there is no configuration, the following error will be reported during compilation:

WeChat Mini Program Network Request Detailed Description

Configuration method:

Set the domain name

You need to set the domain name in the mini program of the WeChat public platform. You can see the setting options in the setting interface of the WeChat applet:

WeChat Mini Program Network Request Detailed Description

Select development settings:

WeChat Mini Program Network Request Detailed Description

You can see the server settings:

WeChat Mini Program Network Request Detailed Description

#Here we can set the domain names that our APPID can access, and we can set up to two of each type. (Note that only https domain names can be used here. This application process takes a certain amount of time)

2. When there is no APPID, it is much more convenient. You can make network requests at will without limiting the domain name. , however, publishing or previewing on mobile phones is not possible in this case. If you want to formally develop small programs, you still need an https domain name, but http is enough for learning.

In the mini program, network requests are roughly divided into four types.

  • Ordinary HTTPS request (wx.request)

  • Upload file (wx.uploadFile)

  • Download file (wx.downloadFile)

  • WebSocket communication (wx.connectSocket)

Here we mainly talk about wx.request:

Use wx.request to initiate an http request. A WeChat applet is limited to only 5 network requests at the same time. Note that it is at the same time.

    wx.request({
      url: 'http://192.168.1.137:80/app/guanggao',
      method: 'POST',
      data: {
         type: "1"
      },
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        that.setData({
          images: res.data.data.guanggao
        })
      }
      fail:function(err){
        console.log(err)
      }
    })

The above code will send an http get request, and the parameters are relatively easy to understand.

  • url The url address of the server

  • data The requested parameters can be String data:”xxx=xxx&xxx=xxx ” form or Object data:{“userId”:1} form

  • header Set the request header

  • Method http method, the default is GET request

  • success InterfaceSuccess callback

  • fail Interface failure callback

There is another parameter that is not in the code:

  • complete is the callback after calling the interface. Regardless of success or failure, the interface will be Call

Timeout setting

As mentioned in the previous article, setting networkTimeout in app.js can set the timeout for four types of network access. Time:


"networkTimeout":{
  "request": 10000,
  "connectSocket": 10000,
  "uploadFile": 10000,
  "downloadFile": 10000
}

The above is the detailed content of WeChat Mini Program Network Request Detailed Description. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor