• uni-app:解决异步请求返回值问题


    可以使用 Promise 或者回调函数来处理异步请求的返回值。

    方法一: Promise处理异步请求的返回值

    使用 Promise 可以将异步请求的结果通过 resolve reject 返回,然后通过 .then() 方法获取成功的结果,通过 .catch() 方法获取错误信息。

    一、在common.js中写入请求后台的操作

    核心代码

    return new Promise((resolve, reject) => {

            //请求操作

    });

    success: res => {

            console.log('请求成功')
            resolve(res.data); // 返回成功的结果
    },
    fail(res) {
            console.log(res)
            reject(res); // 返回失败的结果
     }

     完整代码

    1. //http方式进行登录
    2. function login_httpmode(username, password, cmd) {
    3. return new Promise((resolve, reject) => {
    4. uni.request({
    5. url: ip + 'sys/user/login',
    6. data: {
    7. cmd: cmd,
    8. usrname: username,
    9. passwd: password
    10. },
    11. method: 'POST',
    12. dataType: 'json',
    13. header: {
    14. "content-type": "application/json"
    15. },
    16. success: res => {
    17. console.log('请求成功')
    18. resolve(res.data); // 返回成功的结果
    19. },
    20. fail(res) {
    21. console.log(res)
    22. reject(res); // 返回失败的结果
    23. }
    24. });
    25. });
    26. }

    二、在页面调用common.js中的方法

    核心代码 

    // 调用方法
    common.login_httpmode(username, password, cmd)
            .then(info => {
                    console.log('获取方法中的返回值',info); // 获取到正确的返回值
            })
            .catch(error => {
                     console.log(error); // 获取到错误信息
            });

    完整代码

    1. <template>
    2. <view>
    3. <button @click="getdata">点我获取异步请求数据button>
    4. view>
    5. template>
    6. <script>
    7. import common from "@/utils/common.js"
    8. export default {
    9. data() {
    10. return {
    11. };
    12. },
    13. methods: {
    14. getdata(){
    15. //定义几个测试数据
    16. var username = "admin";
    17. var password = "admin"
    18. var cmd = "124"
    19. // 调用方法
    20. common.login_httpmode(username, password, cmd)
    21. .then(info => {
    22. console.log('获取方法中的返回值',info); // 获取到正确的返回值
    23. //下面可以执行获取到异步数据的方法
    24. if(info.success == true){
    25. console.log("登录数据正确")
    26. }
    27. else{
    28. console.log("登录数据错误")
    29. }
    30. })
    31. .catch(error => {
    32. console.log(error); // 获取到错误信息
    33. });
    34. }
    35. },
    36. };
    37. script>
    38. <style>
    39. style>

    方法二:回调函数来处理异步请求的返回值

    使用回调函数时,将回调函数作为参数传递给异步请求方法,在请求成功时调用 callback(null, res.data) 返回成功的结果,在请求失败时调用 callback(res) 返回错误信息。

    一、在common.js中写入请求后台的操作

    核心代码

    //参数中写入 callback

    function login_httpmode(username, password, cmd, callback) {}

    //请求成功与失败时

    success: res => {
          callback(null, res.data); // 返回成功的结果
        },
        fail: res => {
          callback(res); // 返回失败的结果
        }

    完整代码

    1. function login_httpmode(username, password, cmd, callback) {
    2. uni.request({
    3. url: ip + 'sys/user/login',
    4. data: {
    5. cmd: cmd,
    6. usrname: username,
    7. passwd: password
    8. },
    9. method: 'POST',
    10. dataType: 'json',
    11. header: {
    12. "content-type": "application/json"
    13. },
    14. success: res => {
    15. console.log('请求成功');
    16. console.log(res.data);
    17. callback(null, res.data); // 返回成功的结果
    18. },
    19. fail: res => {
    20. console.log(res);
    21. callback(res); // 返回失败的结果
    22. }
    23. });
    24. }

    二、在页面调用common.js中的方法

    核心代码

    common.login_httpmode(username, password, cmd, (error, info) => {
            if (error) {
                    console.log(error); // 获取到错误信息
            } else {
                    console.log(info); // 获取到正确的返回值
            }
    });

    完整代码

    1. <template>
    2. <view>
    3. <button @click="getdata">点我获取异步请求数据button>
    4. view>
    5. template>
    6. <script>
    7. import common from "@/utils/common.js"
    8. export default {
    9. data() {
    10. return {
    11. };
    12. },
    13. methods: {
    14. getdata(){
    15. //定义几个测试数据
    16. var username = "admin";
    17. var password = "admin"
    18. var cmd = "124"
    19. // 调用方法
    20. common.login_httpmode(username, password, cmd, (error, info) => {
    21. if (error) {
    22. console.log(error); // 获取到错误信息
    23. } else {
    24. console.log(info); // 获取到正确的返回值
    25. }
    26. });
    27. }
    28. },
    29. };
    30. script>
    31. <style>
    32. style>

  • 相关阅读:
    puttygen工具ppk文件版本配置
    SSH Tunneling隧道 - 探究与实践
    Mysql中DQL(查询类)语句的执行顺序
    概率论与数理统计(1)——基本概念
    力扣(LeetCode)97. 交错字符串(C++)
    RKE2部署高可用Rancher2.6.5
    golang学习笔记——missing go.sum entry for go.mod file
    MySql分区介绍和Range案例
    Spring Boot Thymeleaf(十一)
    【Spring源码】9. 重要的ConfigurationClassPostProcessor
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/134007859