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!

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor
