api.txlSearApi(params, this.data.id,decodeURI((search))).then((res)=>{ console.log(res,'这是筛选的数据') })我怎么让search不乱码
时间: 2024-03-22 20:41:38 浏览: 11
在 JavaScript 中,可以使用 `encodeURI` 函数将字符串进行编码,以便在 URL 中传递参数时不会出现乱码。因此,在传递参数时,可以使用 `encodeURI` 函数将参数进行编码,然后在接收参数时使用 `decodeURI` 函数将参数进行解码。
在你的代码中,`search` 参数可能是中文或其他特殊字符,所以可以使用 `encodeURI` 函数对它进行编码,例如:
```
api.txlSearApi(params, this.data.id, encodeURI(search)).then((res)=>{
console.log(res,'这是筛选的数据');
})
```
在服务端接收到请求后,需要对 URL 中的参数进行解码。在 Node.js 中,可以使用 `decodeURIComponent` 函数进行解码,例如:
```
const search = decodeURIComponent(req.query.search);
```
这样,就可以避免 URL 中传递参数时乱码的问题。
相关问题
vue中window.location.href
在Vue中,使用window.location.href进行页面跳转可能会失效。这是因为在提交表单后,页面会刷新并且跳转到表单提交的目标页面,而不是执行window.location.href的跳转操作。为了解决这个问题,可以将按钮的类型从submit改为button。这样点击按钮时不会触发表单的提交,而是执行window.location.href的跳转操作。[1]
在Vue中,可以通过以下代码实现页面跳转并传递参数:
```javascript
methods: {
LookOrderGrab(item) {
let str = JSON.stringify(item);
window.location.href = "/Shop/WB/LookOrderGrab?item=" + encodeURI(encodeURI(str));
},
},
```
在跳转的目标页面中,可以通过以下代码接收参数:
```javascript
created() {
let that = this;
that.$nextTick(() => {
var item = that.GetRequest().item;
var obj = JSON.parse(decodeURI(decodeURI(item)));
console.log(obj);
});
},
methods: {
GetRequest() {
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
},
},
```
以上代码中,LookOrderGrab方法用于跳转页面并传递参数,GetRequest方法用于获取跳转后的页面中携带的参数。注意在跳转时需要对参数进行编码和解码,以确保参数的正确传递和解析。[2][3]
window.location.href传递参数
可以使用window.location.href进行页面跳转并传递参数。例如,可以使用以下代码将参数传递给新页面:
window.location.href = "./punch/clock_frm.html?modFlag=" + modFlag + "&role=" + role;
在新页面接收参数并将其转换为可用的JSON格式,可以使用以下方法:
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") ! -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
}
console.log(theRequest); //此时的theRequest就是我们需要的参数
}
如果在传递参数时遇到中文乱码问题,可以使用encodeURI对中文进行编码,如下所示:
window.location.href = 'aaa.html?Unit=' + encodeURI(encodeURI(中文内容));
在接收页面接收参数时,可以使用decodeURI解码回来,如下所示:
var param = decodeURI(window.location.href);<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [转载--window.location.href传参/传值汇总](https://blog.csdn.net/weixin_47382065/article/details/120264692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [window.open 以post方式传递参数示例代码](https://download.csdn.net/download/weixin_38655011/13621948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]