axios({ method: 'get', url: '/BookShopSystem_war/getQueryBook/' + encodeURIComponent(bookName)
时间: 2024-02-14 14:21:17 浏览: 18
这段代码使用了Axios库发送一个GET请求,请求的url中包含了查询的书名参数。为了确保传递的参数不会出现乱码,使用了JavaScript内置的encodeURIComponent()函数对参数进行编码。这个函数会将特殊字符进行编码,例如将中文字符编码为%xx的形式,保证了参数的正确传递。后端接收到请求后,需要使用相应的解码函数对参数进行解码,以获取正确的参数值。
相关问题
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("取消收藏");
});
}
```
这是vue中的前端代码axios({ method: 'get', url: '/BookShopSystem_war/getQueryBook/' + bookName <select id="getQueryBook" parameterType="String" resultMap="bookResult"> select * from book where book_name like CONCAT('%',#{bookName},'%') </select> 传递的bookName是String但最后会乱码导致查询不到书籍,如何解决
出现乱码的原因可能是因为在HTTP请求中未对参数进行编码。我们可以使用JavaScript的encodeURIComponent()函数来对参数进行编码,以确保它们被正确地传输。
在你的代码中,将`/BookShopSystem_war/getQueryBook/' + bookName`替换为`/BookShopSystem_war/getQueryBook/' + encodeURIComponent(bookName)`应该可以解决你的问题。这将对bookName进行编码,使其在HTTP请求中正确传输,从而避免了出现乱码。