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

Top 10 basic algorithms used by programmers

[[188736]] Algorithm 1: Quick sort algorithm Quic...

There are hidden safety risks when letting children wear silver jewelry!

"He is in the kitchen, with a round purple f...

[Li Jiaoshou] I have 3 ways to extend the life cycle of Internet celebrities

Indeed, many internet celebrities nowadays (such ...

6 minutes to fully understand the App message push strategy

Proper use of push can help product operators ach...

A complete Douyin short video promotion plan, take it for reference~

A very complete Douyin short video promotion plan...

Post-WeChat Era: Disputes over Social Apps

[[149252]] WeChat has become a tool that people r...

How is the “Cross Tide” formed?

On July 30, a video of two tidal waves in the Qia...

The method used by BAT, detailing the pitfalls of A/B testing!

An important idea mentioned in the growth hacker ...

What do you think about bringing Face ID to Mac computers?

Face ID is undoubtedly a highlight of Apple's...

Super complete and detailed! 4 important links of user operation!

The first thing to be clear is that the links of ...