uniapp中uni.request数据发送spring boot
时间: 2023-12-06 11:05:02 浏览: 158
在uniapp中使用uni.request发送数据到Spring Boot后台的步骤如下:
1. 在uniapp项目中使用uni.request发送请求
```javascript
uni.request({
url: 'http://localhost:8080/api/getData',
method: 'POST',
data: {
name: 'test'
},
success: (res) => {
console.log(res.data)
},
fail: (err) => {
console.log(err)
}
})
```
2. 在Spring Boot后台编写接收请求的Controller
```java
@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping("/getData")
public String getData(@RequestBody Map<String, Object> params) {
String name = (String) params.get("name");
return "Hello " + name;
}
}
```
3. 在Spring Boot后台启动应用程序并监听来自前端的请求
```java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
这样就可以在uniapp中使用uni.request发送数据到Spring Boot后台了。注意需要确保前后端的请求地址和请求方式都匹配,同时在后台Controller中使用`@RequestBody`注解将请求的数据解析成Map对象。
阅读全文
相关推荐


















