Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use wx.request to initiate a request in Mini Program

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you how to use wx.request to initiate requests in Mini Program. The content is simple and clear. I hope it can help you solve your doubts. Now let the editor take you to study and learn "how to use wx.request to initiate requests in Mini Program".

Wx.request (OBJECT)

OBJECT parameter description:

The required parameter name type indicates that urlString is the developer server interface address dataObject, String No request parameter headerObject No set request header, ReferermethodString cannot be set in header, default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTdataTypeString are json by default. If dataType is set to json, an attempt will be made to JSON.parsesuccessFunction the response data. Do you receive the callback function returned by the developer service successfully? res = {data: 'content returned by the developer server'} failFunction No API call failed callback function completeFunction No API call ended callback function (both successful and failed calls will be executed)

Success return parameter description:

Parameter indicates the data returned by the minimum version of data developer server, the status code returned by statusCode developer server, the HTTP Response Header1.2.0 returned by header developer server

The data data indicates that the data eventually sent to the server is of type String, and if the incoming data is not of type String, it will be converted to String. The conversion rules are as follows:

For data whose header ['content-type'] is' application/json', the data is serialized by JSON

For data where header ['content-type'] is' application/x-www-form-urlencoded', the data is converted to query string (encodeURIComponent (k) = encodeURIComponent (v) & encodeURIComponent (k) = encodeURIComponent (v)...).

Sample code:

Wx.request ({url: 'test.php', / / is only an example, not the real interface address data: {x:', y:'}, header: {"Content-Type": "application/json"}, success: function (res) {console.log (res.data)}}))

Return value:

The basic library 1.4.0 is supported, and the lower version needs to be compatible.

Returns a requestTask object that interrupts the request task through requestTask.

List of methods for the requestTask object:

Method parameters describe the minimum version of abort interrupt request task 1.4.0

Sample code:

Const requestTask = wx.request ({url: 'test.php', / / is only an example, not the real interface address data: {x:', y:'}, header: {'content-type':' application/json'}, success: function (res) {console.log (res.data)}}) requestTask.abort () / / cancel the request task Bug & Tip

Tip: content-type defaults to 'application/json'

Bug: developer tool version 0.10.102800, content-type setting exception of header

Tip: the HTTPS TLS version of the client is 1.2, but some models of Android do not yet support TLS 1.2, so make sure that the TLS version of the HTTPS server supports 1.2 or less.

Tip: note that the value of method must be uppercase (for example: GET)

Tip: there cannot be ports in url

Tip: both the default timeout and the maximum timeout for request are 60s

Tip: the maximum number of concurrency of request is 5

Tip: the referer of a network request cannot be set, and the format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, where {appid} is the appid of Mini Program, {version} is the version number of Mini Program, and a version number of 0 indicates the development version.

What is the advantage of Mini Program Mini Program compared with developers, Mini Program is less difficult to develop, can save development and operating costs, so that developers can quickly develop a Mini Program. And Mini Program can be easily obtained and spread in Wechat, which can meet the basic life applications, in line with life services offline stores and non-rigid needs of low-frequency applications. For users, it can save time cost and mobile phone memory space, and have excellent use experience at the same time.

The above is about "how to use wx.request to make a request in Mini Program". If this article is helpful and well written, please share it with your friends to learn new knowledge. If you want to know more about it, please pay more attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report