亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet Mini Program Development How to correctly call the backend interface of the applet

How to correctly call the backend interface of the applet

Feb 20, 2021 am 10:42 AM
Applets interface

How to correctly call the backend interface of the applet

Written before:

When I was learning how to request the backend interface recently, I originally planned to request the interface being used in the project, but the WeChat applet did not allow it. The official reminder is that the interface must have a domain name and be registered, but our interfaces are all IP addresses, so there is nothing we can do about it.

However, we don’t have to be frustrated. Let’s share the official request interface method for your reference.

1. Official method

This method is simple and easy to understand, but the amount of code is too large when used. I suggest that it is better to encapsulate it.

wx.request({
  url: 'test.php', // 僅為示例,并非真實的接口地址
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // 默認值
  },
  success(res) {
    console.log(res.data)
  }
})

2. Request method encapsulation (create a new folder util, tool file, create request.js file under the folder, used to encapsulate the method) request.js is as follows:

var app = getApp();
//項目URL相同部分,減輕代碼量,同時方便項目遷移
//這里因為我是本地調(diào)試,所以host不規(guī)范,實際上應(yīng)該是你備案的域名信息
var host = 'http://localhost:8081/demo/';

/**
 * POST請求,
 * URL:接口
 * postData:參數(shù),json類型
 * doSuccess:成功的回調(diào)函數(shù)
 * doFail:失敗的回調(diào)函數(shù)
 */
function request(url, postData, doSuccess, doFail) {
  wx.request({
    //項目的真正接口,通過字符串拼接方式實現(xiàn)
    url: host + url,
    header: {
      "content-type": "application/json;charset=UTF-8"
    },
    data: postData,
    method: 'POST',
    success: function (res) {
      //參數(shù)值為res.data,直接將返回的數(shù)據(jù)傳入
      doSuccess(res.data);
    },
    fail: function () {
      doFail();
    },
  })
}

//GET請求,不需傳參,直接URL調(diào)用,
function getData(url, doSuccess, doFail) {
  wx.request({
    url: host + url,
    header: {
      "content-type": "application/json;charset=UTF-8"
    },
    method: 'GET',
    success: function (res) {
      doSuccess(res.data);
    },
    fail: function () {
      doFail();
    },
  })
}

/**
 * module.exports用來導出代碼
 * js文件中通過var call = require("../util/request.js")  加載
 * 在引入引入文件的時候"  "里面的內(nèi)容通過../../../這種類型,小程序的編譯器會自動提示,因為你可能
 * 項目目錄不止一級,不同的js文件對應(yīng)的工具類的位置不一樣
 */
module.exports.request = request;
module.exports.getData = getData;

( Learning video sharing: Introduction to Programming)

3. Create a folder in the page, create four files, add

1 //引入代碼
 2 var call = require("../util/request.js")
 3 
 4 Page({
 5   data: {
 6     pictureList: [],
 7   },
 8   
 9   onLoad: function () {
10     var that = this;
11     //調(diào)用封裝的方法,為了方便我直接在頁面加載的時候執(zhí)行這個方法
12     call.getData('lunbo.do', this.shuffleSuc, this.fail);
15   },
16   shuffleSuc: function (data) {
17     var that = this;
18     that.setData({
19       pictureList: data.rows
20     })
21     //我后面測試了一下,直接this.setData也可以,但是因為我在沒有使用封裝方法的時候
22     //this.setData報過錯,不能直接用this,所以我在賦值的時候一般都會加上var that = this;
23     //這句話算是一個不是習慣的習慣
24   },
25   fail: function () {
26     console.log("失敗")
27   },
28 })

to js and write the callback function in In the page, when calling the encapsulated method, call it through the this. method name, so as to ensure that the that.setData method is valid.

Related recommendations: Mini Program Development Tutorial

The above is the detailed content of How to correctly call the backend interface of the applet. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard Mar 12, 2024 pm 04:34 PM

When we assemble the computer, although the installation process is simple, we often encounter problems in the wiring. Often, users mistakenly plug the power supply line of the CPU radiator into the SYS_FAN. Although the fan can rotate, it may not work when the computer is turned on. There will be an F1 error "CPUFanError", which also causes the CPU cooler to be unable to adjust the speed intelligently. Let's share the common knowledge about the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard. Popular science on the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard 1. CPU_FANCPU_FAN is a dedicated interface for the CPU radiator and works at 12V

Common programming paradigms and design patterns in Go language Common programming paradigms and design patterns in Go language Mar 04, 2024 pm 06:06 PM

As a modern and efficient programming language, Go language has rich programming paradigms and design patterns that can help developers write high-quality, maintainable code. This article will introduce common programming paradigms and design patterns in the Go language and provide specific code examples. 1. Object-oriented programming In the Go language, you can use structures and methods to implement object-oriented programming. By defining a structure and binding methods to the structure, the object-oriented features of data encapsulation and behavior binding can be achieved. packagemaini

Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Introduction to PHP interfaces and how to define them Introduction to PHP interfaces and how to define them Mar 23, 2024 am 09:00 AM

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Solution to NotImplementedError() Solution to NotImplementedError() Mar 01, 2024 pm 03:10 PM

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

How to get membership in WeChat mini program How to get membership in WeChat mini program May 07, 2024 am 10:24 AM

1. Open the WeChat mini program and enter the corresponding mini program page. 2. Find the member-related entrance on the mini program page. Usually the member entrance is in the bottom navigation bar or personal center. 3. Click the membership portal to enter the membership application page. 4. On the membership application page, fill in relevant information, such as mobile phone number, name, etc. After completing the information, submit the application. 5. The mini program will review the membership application. After passing the review, the user can become a member of the WeChat mini program. 6. As a member, users will enjoy more membership rights, such as points, coupons, member-exclusive activities, etc.

What is the difference between interfaces and abstract classes in PHP? What is the difference between interfaces and abstract classes in PHP? Jun 04, 2024 am 09:17 AM

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.

What is the difference between an abstract class and an interface in PHP? What is the difference between an abstract class and an interface in PHP? Apr 08, 2025 am 12:08 AM

The main difference between an abstract class and an interface is that an abstract class can contain the implementation of a method, while an interface can only define the signature of a method. 1. Abstract class is defined using abstract keyword, which can contain abstract and concrete methods, suitable for providing default implementations and shared code. 2. The interface is defined using the interface keyword, which only contains method signatures, which is suitable for defining behavioral norms and multiple inheritance.

See all articles