【发布时间】:2012-08-23 12:22:28
【问题描述】:
我正在构建一个进程,其中 js 对象通过 ajax(POST 和 json 类型)提交到 php 文件,但在迭代 php 中提交的内容时遇到问题。
我的对象如下所示:
var myObject = {
"section1":{
"subitem1":"value1",
"subitem2":"value2"
},
"section2":{
"subitem3":"value3",
"subitem4":"value4"
}
}
我的 ajax 看起来像这样:
$.ajax({
url:"test.php?section=section2",
type:"POST",
dataType:"json",
success:function(data){
// what i do with the response data
},
data:myObject
});
这是我的 php:
$section = $_GET['section'];
$json = json_decode($_POST[$section], true);
foreach($json as $key=>$value){
//if this iteration works here, it'll be the happiest point of my day
}
现在,在上面的 php 中,如果我将某个部分称为 $_POST['section2'],那么迭代确实有效。所以使用 PHP 的 variables 变量似乎是个问题,但我不知道....整个 $_POST 似乎也作为一个对象出现。 JQUERY AJAX 会自动对我提交的对象执行 JSON.stringify 吗?我试过使用 stringify 但它没有用..我有最新版本的 chrome...
另外,我也尝试在 $_POST 上使用 json_decode...仍然 $_POST[$section] 被解释为 null...
非常感谢任何帮助、建议和建议!
【问题讨论】:
-
您能否对 $_POST 数组进行 var_dump 以便我们查看它是如何接收数据的?
标签: php javascript json object casting