As a cross-platform development framework, Uniapp’s network request function is an integral part of it, because mobile applications need to continuously obtain data from the server and display it on the user’s device. When developing with Uniapp, the network request interface is very simple. You can use the official network request API or a third-party network request library provided by uni-app. However, for beginners, some details of the network request results may not be clear. This article will introduce in detail the relevant content of Uniapp network request results.
Uniapp official network request API
uni.request(object) is the official Uniapp network request API, which can send HTTP/HTTPS requests and return data. Its syntax is as follows:
uni.request({
url: '',
method: '',
data: {},
header: {},
success: res => {},
fail: () => {},
complete: () => {}
})
where the url is Required, representing the requested address; method is optional, representing the request method, and the default is GET; data is optional, representing the requested data; header is optional, representing the request header information; success, fail, and complete are also optional Options, respectively representing callback functions after request success, failure and completion.
The above is the most basic network request configuration example. You can also set the request timeout, response data type, certificate verification, etc. Below we will explain how to obtain the results of the network request after the request is completed.
Network request result structure
After the network request is successful, the server will return a response result. In Uniapp, the response result is stored in the data attribute of the response object. The specific structure is shown in the figure below:
The response status code is stored in the statusCode attribute, and the meaning of the status code can be queried through the HTTP protocol. Response header information is stored in the header attribute. The response data is stored in the data attribute, and its data type is string or ArrayBuffer type, which can be converted to object type through JSON.parse().
Uniapp obtains the network request result
When the server responds to the request successfully, the success callback function is executed. At this time, the response result can be obtained through the parameters of the function.
uni.request({
url: 'url', success: function(res) { console.log(res.data); // 响应数据 console.log(res.statusCode); // 响应状态码 console.log(res.header); // 响应头信息 }, fail: function(res) { console.log(res.errMsg); // 错误信息 }
})
The res here is the network request result object. In the success callback function, you can pass res.data, res. Attributes such as statusCode and res.header obtain the response result of the network request.
Response data conversion
Since the data format returned by the network request is uncertain and may be JSON string, XML string or other format data, the response data needs to be converted. Uniapp has a built-in JSON.parse() method that can convert JSON strings into object types. If the response data is not in JSON format, other conversion methods can be used. For example, XML data needs to be converted using the xml2js library.
uni.request({
url: 'url',
success: function(res) {
var jsonStr = res.data; var jsonObj = JSON.parse(jsonStr); // 将JSON字符串转成JSON对象
},
fail: function(res) {
console.log(res.errMsg);
}
})
Exception handling
Network requests may also encounter abnormal situations, such as network unavailability, server failure and other errors. The fail callback function in Uniapp is triggered when an error occurs in a network request and can be processed based on the error information.
uni.request({
url: 'url',
success: function(res) {
console.log(res.data);
},
fail: function(res) {
console.log(res.errMsg); // 打印错误信息
}
})
Summary
Through the above introduction, we know the structure of Uniapp network request results, how to obtain the results of network requests, response data conversion and Exception handling and other related content. Network requests are an indispensable part of mobile application development. It can help us display data in a diverse way, provide users with a richer and more dynamic experience, and also help improve the interactivity and flexibility of the application.
The above is the detailed content of uniapp network request results. For more information, please follow other related articles on the PHP Chinese website!