我想知道如何在 Javascript 中将变量作为 +4
传递,然后在 php 中将其作为 +4
检索。
无论我做什么,每当我存储一个+4"
,并验证它是这样存储的,当我检索它时,+
就消失了,我所有的get 是数字(即 4
)。
这是我得到的:
文件.js
var testString = "+4";
console.log(testString);
window.location.href = "file.php?testString=" + testString;
输出:+4
文件.php
$testString = $_GET["testString"];
echo $testString;
输出:4
我知道我总是可以附加“+”,但这似乎是一个不必要的额外过程;我宁愿正确传递变量。
最佳答案
您需要encode你的参数:
var testString = "+4";
console.log(testString);
window.location.href = "file.php?testString=" + encodeURIComponent(testString);
有几个问题/资源可以帮助您理解为什么需要这样做:
关于javascript - 通过查询字符串传递变量 chop 加号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662039/