Use Fly to initiate http requests in WeChat applet

Use Fly to initiate http requests in WeChat applet

The javascript running environment of WeChat applet is different from that of the browser. The script logic of the page runs in JsCore, which is an environment without window objects. Therefore, window cannot be used in the script, and components cannot be operated in the script. There is no XmlhttpRequest object in JsCore, so jquery, zepto, axios cannot be used in the applet. This is when fly comes into play.

You need to download wx.js (uncompressed) or wx.umd.min.js (compressed, 12k) from https://unpkg.com/flyio/dist/ or https://github.com/wendux/fly/tree/master/dist, and then copy it to your project directory.

[[207726]]

use

  1. var Fly = require ( "../lib/wx.js" ) //wx.js is the source code file you downloaded
  2. var fly = new Fly(); create a fly instance
  3. ...
  4. Page({
  5. //Event processing function
  6. bindViewTap: function () {
  7. //Call
  8. fly.get( "http://10.10.180.81/doris/1/1.0.0/user/login" ,{xx:6}). then ((d)=>{
  9. console.log(d.data)
  10. }).catch(err=>{
  11. console.log(err.status,err.message)
  12. })
  13. })
  14. })
  15.  
  16.  
  17. Author: lazydu
  18. Link: http://www.jianshu.com/p/2d0a1ad94ed5
  19. Source: Jianshu
  20. The copyright belongs to the author. For commercial reproduction, please contact the author for authorization. For non-commercial reproduction, please indicate the source.

If you are just a simple user, you don't need to read on, just stop here, don't close it in a hurry, come and star it before leaving https://github.com/wendux/fly. If you are interested in the principle, the following introduces the principle behind it.

principle

Fly's support for mini-programs is actually through a custom http engine. Let's take a look at the wx.js source code:

  1. // WeChat applet entry
  2. var Fly=require( "../dist/fly" )
  3. var EngineWrapper = require( "../dist/engine-wrapper" )
  4. var adapter = require( "../dist/adapter/wx" ) //WeChat applet adapter
  5. var wxEngine = EngineWrapper(adapter)
  6. module.exports= function (engine) {
  7. return new Fly(engine||wxEngine);
  8. }
  9.  
  10.  
  11. Author: lazydu
  12. Link: http://www.jianshu.com/p/2d0a1ad94ed5
  13. Source: Jianshu
  14. The copyright belongs to the author. For commercial reproduction, please contact the author for authorization. For non-commercial reproduction, please indicate the source.

It can be seen that the key code is in adapter/wx. Let's take a look at the adapter code of the WeChat applet:

  1. //WeChat applet adapter
  2. module.exports= function (request, responseCallback) {
  3. var con = {
  4. method: request.method,
  5. url: request.url,
  6. dataType: request.dataType|| "text" ,
  7. header: request.headers,
  8. data: request.body||{},
  9. success(res) {
  10. responseCallback({
  11. statusCode: res.statusCode,
  12. responseText: res.data,
  13. headers: res.header,
  14. statusMessage: res.errMsg
  15. })
  16. },
  17. fail(res) {
  18. responseCallback({
  19. statusCode: res.statusCode||0,
  20. statusMessage: res.errMsg
  21. })
  22. }
  23. }
  24. //Call the WeChat API to make a request
  25. wx.request(con)
  26. }
  27.  
  28.  
  29. Author: lazydu
  30. Link: http://www.jianshu.com/p/2d0a1ad94ed5
  31. Source: Jianshu
  32. The copyright belongs to the author. For commercial reproduction, please contact the author for authorization. For non-commercial reproduction, please indicate the source.

That's all the implementation, very simple! This example can help you understand the sentence "Fly supports different environments through different adapters". As for other environments, we can just follow suit.

<<:  Why does it only take 2 minutes to withdraw a WeChat message?

>>:  [Special Topic] The 10th issue of the Aiti Tribe Technical Clinic: How to learn Python? The method is very important

Recommend

Practical tips: How to increase followers on Douyin?

Recently, some people have been asking, “Why is T...

Protecting Northeast China's black soil and safeguarding China's "rice bowl"

Produced by | Science Popularization China Author...

3 marketing ideas to implement Spring Festival marketing promotion

During this period, there should be many brands a...

How does Tik Tok do content positioning? Share these 4 points!

If you don’t do a good job of content positioning...

Steve Jobs once wanted a TV like this, and Hisense helped him realize it

To date, Apple has not yet produced a complete te...

The 2022 Ig Nobel Prizes are announced! Two awards were won by Chinese teams

At 6 p.m. on September 15, Eastern Time, the 32nd...