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

高洛峰
Release: 2017-02-28 11:36:23
Original
1796 people have browsed it

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:

<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!


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!