gpt4 book ai didi

javascript - 我如何访问我发送到服务器文件的对象

转载 作者:行者123 更新时间:2023-11-30 00:26:38 24 4
gpt4 key购买 nike

//向服务器上的一个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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com