ajax的get方法带参上传时,如果目标带有特殊符号时,会导致参数接收错误,所以需要将字符替换为对应编码
replace函数(替换对象,目标字符)
先到common.js新建一个函数(不懂的可以回头查一下我的往期,有个方法封装的)
const detection = function(req){
//req = '皮沙发保养(3+1+1.5座)'
req = req.replace(/\%/g, '%25')
req = req.replace(/\+/g, '%2B')
req = req.replace(/ /g, '%20')
req = req.replace(/\?/g, '%3F')
req = req.replace(/\//g, '%2F')
req = req.replace(/\#/g, '%23')
req = req.replace(/\&/g, '%26')
req = req.replace(/\=/g, '%3D')
console.log(req)
//皮沙发保养(3%2B1%2B1.5座)
return req
}
module.exports = {
detection:detection
}
let req = detection(req)
console.log(req)
使用时引入传值使用就OK了