uni-app calling interface steps: define the request method, including URL, method and data; set the request header (optional); send the request; response processing, including success and failure callbacks.
How to use uni-app to call the interface
Steps:
1. Define the request method
<code class="javascript">const request = uni.request({ url: 'http://example.com/api/v1/users', method: 'GET', data: { name: 'John Doe' }, success: (res) => { console.log(res.data) }, fail: (err) => { console.log(err) } })</code>
Parameter description:
url
: Requested interface addressmethod
: Request method (such as GET, POST, PUT, DELETE) data
: Request parameters (optional) success
: The callback function when the request is successfulfail
: The callback function when the request fails2. Settings Request header (optional)
You can use the setRequestHeader()
method to set the request header:
<code class="javascript">request.setRequestHeader('Content-Type', 'application/json')</code>
3. Send a request
Callsend()
Method to send a request:
<code class="javascript">request.send()</code>
4. Response processing
Process the response of successful request in the success
callback function, in fail
Error in handling request failure in callback function.
Example:
<code class="javascript">uni.request({ url: 'http://example.com/api/v1/users', method: 'GET', success: (res) => { const users = res.data.users console.log(users) }, fail: (err) => { console.log(err) } })</code>
Note:
uni.request()
Yes Asynchronous requests will not block the execution of subsequent code. The above is the detailed content of How to call interface in uniapp. For more information, please follow other related articles on the PHP Chinese website!