//向服务器上的一个php文件发送一个ajax http post请求,post//request是一个简单的对象。
var xhr = new XMLHttpRequest();
var person = {
"firstName" : "Adebowale",
"lastName" : "Johnson",
"ago" : 43
}
xhr.open("POST","phfile.php",true);
xhr.setRequestHeader("Content-type","application/x-www-form- urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
var status = xhr.status;
if((status >= 200) && (status < 300) || (status === 304)) {
alert(xhr.responseText);
}
}
};
xhr.send(JSON.stringify(person));
//如果我做 alert(xhr.responseText); //我从浏览器获取对象{}。
//在服务器上,使用 php,我如何访问对象,如果我做 echo 或//print_r,我得到一个没有任何属性的空对象 --- object{}。
//从我问题的语气中你可以看出,我对所有人都还是很陌生//这些,我只是想学习。
//在我的 phfile.php 上,我设置了以下 php 代码...
<?php
print_r
//How do I access the object I sent to this file please
?>
最佳答案
我看不到您的 AJAX
请求中需要 JSON.stringify(person)
,因为 的所有
已经在keys
对象字符串
中。
因为你使用的是POST
方法,所以你可以像这样直接访问对象
print_r ($_POST['person']);
关于javascript - 我如何访问我发送到服务器文件的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31193733/