我正在構建一個進程,其中js對象通過ajax(POST和json類型)提交給php文件,而且我通過迭代已提交的內容在PHP中。JS對象 - >從jquery ajax提交JSON - > 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的變量變量似乎是問題,但我不知道....整個$ _POST似乎也作爲一個對象來進來。 JQUERY AJAX是否會自動對我提交的對象執行JSON.stringify?我試過使用stringify,但它沒有工作..我有最新版本的鉻...
此外,我試圖在$ _POST上使用json_decode ...仍然$ _POST [$節]被解釋爲空...
任何幫助,建議,建議非常感謝!
你能的var_dump $ _POST數組,所以我們可以看到它是如何recieving的數據? – Ray