[WeChat applet] Use the wx.request method to send a POST network request, carrying the RequestBody parameter

In WeChat applets, you can use wx.requestthe method to send network requests. The following is an example of converting the above Java code into a WeChat applet version:

const url = 'http://..../authorize/login';
const data = {
    
    
  username: '...',
  password: '...'
};

wx.request({
    
    
  url: url,
  method: 'POST',
  data: JSON.stringify(data),
  header: {
    
    
    'Content-Type': 'application/json'
  },
  success: function(res) {
    
    
    // 请求成功,处理返回的数据
    console.log(res.data);
  },
  fail: function(err) {
    
    
    // 请求失败,处理错误信息
    console.error(err);
  }
});

In the above code, we use wx.requestthe method to send a POST request, and set the requested URL, request body data, request header and other information accordingly. successAfter the request is successful, the returned data will be processed in the callback function , and if the request fails, failthe error message will be processed in the callback function.

Please note that the method of the WeChat applet wx.requestwill transmit data in JSON format by default, so we use JSON.stringifythe method to convert the request body data into a string format.

According to actual needs and interface requirements, you may need to modify and adjust the URL, request header, and request body of the request accordingly.

Guess you like

Origin blog.csdn.net/gao511147456/article/details/131632289