Uniapp 的 uni.request传参后端

以下是使用Uniapp的交互数据的两种方式

后端使用@Parameter接收数据

后端使用@RequestBody接收Json格式数据

后端:

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password){
        System.out.println(username);
        System.out.println(password);

        return "登录成功";
    }

    @PostMapping("/login")
    public String jsonLogin(@RequestBody UserDTO userDTO){
        System.out.println(userDTO.getUsername());
        System.out.println(userDTO.getPassword());


        return "登录成功";
    }
}

前端:

import { ref } from 'vue';

let username = ref('')
let password = ref('')

function login() {  
    uni.request({ 
        url: "http://localhost:8080/user/login",   
        data:({   
            username: username.value,
			password: password.value
        }),  
        success: function(res) {  
            console.log(res.data);  
        },  
        fail: function(err) {  
            console.error(err);  
        },
		complete() {

		}
    }); 
	
}


function jsonLogin(){
	uni.request({  
	    url: 'http://localhost:8080/user/login', // 你的后端API地址  
	    method: 'POST',  
	    data: {  
	        username: username.value,  
	        password: password.value  
	    },  

	    success: function (res) {  
	        // 请求成功时的回调函数  
	        console.log(res.data); // 打印后端返回的数据  
	    },  
	    fail: function (error) {  
	        // 请求失败时的回调函数  
	        console.error(error); // 打印错误信息  
	    }  
	});
}

相关推荐

  1. Uniapp uni.request

    2024-03-22 07:48:03       39 阅读
  2. uniapp页面间方法

    2024-03-22 07:48:03       40 阅读
  3. 各种格式混合vue前端

    2024-03-22 07:48:03       46 阅读
  4. uniapp页面怎么

    2024-03-22 07:48:03       39 阅读
  5. uniapp 跨页面几种方式

    2024-03-22 07:48:03       53 阅读
  6. uniapp 实现tabBar-switchTab之间

    2024-03-22 07:48:03       61 阅读
  7. uniapp多张图片到django

    2024-03-22 07:48:03       53 阅读
  8. 前后格式

    2024-03-22 07:48:03       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 07:48:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 07:48:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 07:48:03       87 阅读
  4. Python语言-面向对象

    2024-03-22 07:48:03       96 阅读

热门阅读

  1. IOS面试题编程机制 6-10

    2024-03-22 07:48:03       34 阅读
  2. 如何实现自己的Spring Boot Starter

    2024-03-22 07:48:03       42 阅读
  3. QT网络编程之实现UDP广播发送和接收

    2024-03-22 07:48:03       40 阅读
  4. c++ 构造函数详细介绍

    2024-03-22 07:48:03       42 阅读
  5. 377. 组合总和 Ⅳ

    2024-03-22 07:48:03       42 阅读
  6. 常用的Node.js命令集锦

    2024-03-22 07:48:03       33 阅读
  7. VSCode使用MSVC编译器

    2024-03-22 07:48:03       44 阅读
  8. 导入excel复杂校验加异常信息返回

    2024-03-22 07:48:03       37 阅读
  9. 并网型风光储微电网日前优化调度(MATLAB实现)

    2024-03-22 07:48:03       40 阅读