Home WeChat Applet Mini Program Development WeChat applet development: How to implement network requests (GET requests)

WeChat applet development: How to implement network requests (GET requests)

Feb 28, 2017 am 11:36 AM

The technological changes of the Internet are really turbulent and ever-changing. Fortunately, everything remains unchanged. With the advent of mini programs on January 9, we IT professionals can only keep learning. Internet web development will inevitably use network get requests, so how to implement network requests in WeChat applet development? Today I will talk about the simplest request. I will try to upload, download, and Socket later.

Notes on WeChat applet requests:
1. A WeChat applet can only have 5 network request connections at the same time.
This rule should be formulated by WeChat to ensure user experience. After all, it is a mini program.
2.wx.request(OBJECT) Parameter description:

 微信小程序开发:如何实现网络请求(GET请求)

WeChat The applet supports GET, POST and other requests. It can be set using method.
The following is the code for the GET request:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<span style="font-size:18px;">//rate.js 

//获取应用实例 

var app = getApp() 

Page( { 

  data: { 

    code: &#39;USD&#39;, 

    currencyF_Name: &#39;&#39;, 

    currencyT_Name: &#39;&#39;, 

    currencyF: &#39;&#39;, 

    currencyT: &#39;&#39;, 

    currencyFD: 1, 

    exchange: 0, 

    result: 0, 

    updateTime: &#39;&#39;, 

  }, 

  onLoad: function( options ) { 

    var that = this; 

      //获取汇率 

      wx.request( { 

        url: "http://op.juhe.cn/onebox/exchange/currency?key=我的appkey&from=CNY&to="+code, 

        success: function( res ) { 

          that.setData( { 

            currencyF_Name: res.data.result[0].currencyF_Name, 

            currencyT_Name: res.data.result[0].currencyT_Name, 

            currencyF: res.data.result[0].currencyF, 

            currencyT: res.data.result[0].currencyT, 

            currencyFD: res.data.result[0].currencyFD, 

            exchange: res.data.result[0].exchange, 

            result: res.data.result[0].result, 

            updateTime: res.data.result[0].updateTime, 

          }) 

        

      }) 

  

})</span>

Copy after login

In the above code, you only need to give the URL. The onLoad function is started when the page is initialized. The res.data of success in wx.request({}) is the data obtained from the background. This needs to be noted.
The following is the format of the obtained json data.

 微信小程序开发:如何实现网络请求(GET请求)

You don’t need to do the parsing of json yourself. When I was doing Android, I had to use gson or fastjson to parse json. WeChat has indeed saved a lot of development costs for our developers. Is this a good thing or a bad thing for developers? .

For more WeChat applet development: How to implement network requests (GET requests), please pay attention to the PHP Chinese website for related articles!


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

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

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)