尽管在论坛中听说 PHP 会自动转换通过 AJAX GET 或 POST 方法发送的编码字符串。我使用 ajax post 方法发送数据,该方法使用 encodeURIComponent() 函数编码。我的代码段是:
var name = document.getElementById("name").value;
var url = "decode.php";
var param = "name=" + name;
ajaxRequest.open("POST", url, true);
//Send the proper header information along with the request
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", param.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(encodeURIComponent(param));
当服务器收到它时,如果我尝试回显它(结果通过 ajax 响应显示),它只显示空字符串:
<?php
$content = $_POST['name'];
echo $content;
?>
如何让 PHP 返回正确的输入字符串?我很困惑!