<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>解决url传递过程中加号变空格的问题</title>
</head>
<body>
<button id="btn">点击</button>
<script src="./utils/jquery.js"></script>
<script>
// 获取id为btn的按钮
const btn = document.getElementById("btn");
// 给按钮绑定点击事件
btn.onclick = function () {
$.ajax({
type: "get",
// 错误的
// url: 'http://192.168.80.57:8030/system/captcha?key=MAS+SKL',
// 正确的
url: 'http://192.168.80.57:8030/system/captcha?key=' + encodeURIComponent('MAS+SKL'),
success: function (res) {
console.log("res", res);
},
error: function (err) {
console.log("err", err);
}
})
}
</script>
</body>
</html>
+号变成空格的原因:
使用encodeURIComponent即可解决。