this.axios({ method: "POST", url: "/register", data: userInfo, }) .then((result) => { // console.log(result); this.$toast.clear(); if (result.data.code == 100) { // 注册成功 this.isShow = false; } else { //注册过了,弹窗注册失败信息 this.$toast(result.data.msg); } }) .catch((err) => { this.$toast.clear(); });
时间: 2024-02-10 18:05:31 浏览: 33
这段代码是使用 axios 发送 POST 请求,并在请求成功时清除 Toast,根据响应结果来控制页面的显示和提示信息。其中 `this.$toast.clear()` 是清除之前显示的 Toast,相当于隐藏提示信息。`result.data.code` 表示响应结果中的状态码,`100` 表示注册成功,其他状态码则表示注册失败,此时会弹出提示信息。`this.$toast(result.data.msg)` 表示弹出提示信息,`result.data.msg` 是响应结果中传递的具体提示内容。
相关问题
this.$axios({ url: `/api/login`, method: 'post', data: { ...this.formLabelAlign } }).then(res => { let resData = res.data.data
这段代码使用了Vue.js中的$http插件,即axios库的Vue.js封装版,通过this.$axios来发送一个POST请求到/api/login接口,请求体的数据为this.formLabelAlign对象。this.formLabelAlign对象中的属性值将被解构到请求体中,作为POST请求的数据。
接着,代码调用.then()方法,注册一个回调函数,用于处理请求成功后的响应结果。在回调函数中,将响应结果的data字段解构到resData变量中。
需要注意的是,这里的响应结果是经过axios库处理过的,已经被自动解析为一个JSON对象,可以直接访问data属性。如果服务器返回的是一个字符串,需要先使用JSON.parse()方法将其解析为JSON对象。同时,需要注意处理请求失败的情况,可以在.then()方法后面使用.catch()方法来处理请求失败的情况。
methods: { delCollect(bookId) { var _this = this; axios({ method: "delete", url: "http://localhost:8080/BookShopSystem_war/deleteCollect", data: { userId: _this.userId, bookId: bookId } }).then(resp => { alert("取消收藏"); }); } }该方法还需要userId,请仿照以下接口的方式获取到usrIdmounted() { var _this = this; axios.get('/BookShopSystem_war/getUserId') .then((response) => { this.userId = response.data; var userId = this.userId; // 将userId赋值给一个变量 axios({ method: 'get', url: 'http://localhost:8080/BookShopSystem_war/getCollectBook/' + userId }).then(function (result) { var code = result.data.code; var data = result.data.data; if (code == 200) { _this.collectBook = data; } console.log(data) }) }) .catch((error) => { console.log(error); }); },
你的问题是如何获取 userId,对吧?
对于获取 userId,可以仿照以下接口的方式,在 mounted 钩子函数中使用 axios 发送 get 请求,获取到后将 userId 赋值给一个变量。具体代码如下:
```
mounted() {
var _this = this;
axios.get('/BookShopSystem_war/getUserId')
.then((response) => {
this.userId = response.data;
var userId = this.userId; // 将userId赋值给一个变量
axios({
method: 'get',
url: 'http://localhost:8080/BookShopSystem_war/getCollectBook/' + userId
}).then(function (result) {
var code = result.data.code;
var data = result.data.data;
if (code == 200) {
_this.collectBook = data;
}
console.log(data)
})
})
.catch((error) => {
console.log(error);
});
},
```
在 delCollect 方法中,可以直接使用 this.userId 获取到 userId,具体代码如下:
```
delCollect(bookId) {
var _this = this;
axios({
method: "delete",
url: "http://localhost:8080/BookShopSystem_war/deleteCollect",
data: {
userId: this.userId, // 直接使用 this.userId 获取到 userId
bookId: bookId
}
}).then(resp => {
alert("取消收藏");
});
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)