文件路径及其 URL 变量在连接时被缩短


Filepath and it's URL variable is cut short when concatenated

>我有以下JQuery AJAX函数:

function JQueryAJAXFunction(){
$input = document.getElementById("textInput").value;
console.log($input ); //Console output: "How to"
$('#resultsOutput').load('ajax/serverside/filepath.php?urlvariable='+$input);
} 

它从文本字段中检索$input的值,这是一个两个字母的单词,如"如何",并将其显示在浏览器的控制台上。

然后,$input与服务器端脚本的文件路径连接起来,并分配为 URL 变量。然而,在这样做时,$input的最后一个词 - 在这种情况下是"to" - 消失/被切断。

执行函数后,服务器上的 URL 变量将检索并显示如下方法: $_GET["urlvariable"] 它的值是你可以猜到的"如何"。

建议的解决方案是什么?我怎样才能$_GET["urlvariable"]等于"如何"。

某些字符在 URL 中使用时需要编码...空间就是其中之一。JavaScript 提供了一个全局函数来编码 URL 的任何必要字符 - encodeURIComponent 。不仅仅是连接$input,而是连接:

encodeURIComponent($input)

参考:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent