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

[Li Jiaoshou] How to formulate a program-based marketing plan?

There are two types of marketing plans: one is an...

The father of Moto 360: Changing the world one product at a time

[[138363]] [Key Points] Lior Ron is a Silicon Val...

The future of mobile app development from the history of middleware

In the field of mobile development, we found a ve...

How much does it cost to join a lottery app in Fuyang?

How much does it cost to join a lottery app in Fu...

How to wear an N95 mask? How long can an N95 mask be worn at a time?

Masks are being snatched up everywhere, manufactu...

Tik Tok operation and promotion plan, super detailed!

Recently, Douyin released the "2020 Spring F...

How do content products operate?

How do you explain that product and operation are...