file_in.pdf
}
PHP:
> xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsondata = xmlhttp.responseText;
console.log(xmlhttp.responseText);
document.getElementById("jsondata").value = jsondata;
console.log(innerHTML = jsondata.word);
document.getElementById("approved").innerHTML = "Thank you! We will get back to you.";
}
};
xmlhttp.open("POST", url,false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
xmlhttp.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
xmlhttp.send("a="+a+ "&b="+b);
答案 0 :(得分:1)
首先,您要在请求中发送CORS 响应标题 - 这不是必需的,不做任何事情,除非由于非标准标题而保证预检 - 所以,删除:
use Cake\Auth\DefaultPasswordHasher;
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
其次,您将xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
xmlhttp.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
与同步(已弃用)请求结合使用 - 在下面的代码中,使请求异步,并使用更现代的onreadystatechange
希望有些评论能够让你了解其余的变化
onload
和PHP
var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText); // we get JSON, so parse it to an Object
console.log(xmlhttp.responseText);
document.getElementById("jsondata").value = data;
//console.log(innerHTML = jsondata.word); what is this supposed to do? removed because it looks wrong
}
};
xmlhttp.open("POST", url, true); // use ASYNCHRONOUS request, because it's the 21st century
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send("a="+a+ "&b="+b);
答案 1 :(得分:0)
您的XMLhttprequest.send
未发送空白请求。它实际上发送了一个&湾我通过将一些随机值设置为和来测试您的代码,并且$req
变量实际上填充了a和b的值。
<script>
var xmlhttp = new XMLHttpRequest();
var url = 'http://localhost/test/test.php';
var a= 5;
var b=6;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsondata = xmlhttp.responseText;
console.log(xmlhttp.responseText);
document.getElementById("jsondata").value = jsondata;
console.log(innerHTML = jsondata.word);
document.getElementById("approved").innerHTML = "Thank you! We will get back to you.";
}
};
xmlhttp.open("POST", url,false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
xmlhttp.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
xmlhttp.send("a="+a+ "&b="+b);
</script>
&#13;
当我跑步时,我得到a=5&b=6
。我在你的代码中看到的一个问题是你没有回应结果。
这样做:
$req = file_get_contents("php://input");
echo $req;
根据Jaromanda X的建议,我想指出您没有将JSON数据发送到PHP脚本,因此尝试解码它会产生错误。