Thymeleaf 中 封装 axios 各个方法
时间: 2024-06-14 22:08:50 浏览: 8
Thymeleaf是一种用于Java和Spring框架的模板引擎,它可以与axios结合使用来进行前端页面的异步请求。下面是封装axios各个方法的示例代码:
1. GET请求:
```javascript
function get(url, params) {
return axios.get(url, { params: params })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
```
2. POST请求:
```javascript
function post(url, data) {
return axios.post(url, data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
```
3. PUT请求:
```javascript
function put(url, data) {
return axios.put(url, data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
```
4. DELETE请求:
```javascript
function del(url) {
return axios.delete(url)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
```
这些方法可以根据需要进行修改和扩展,以满足具体的业务需求。通过封装这些方法,可以更方便地在Thymeleaf中使用axios进行异步请求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)