[英]JSON object being passed using JQUERY AJAX cannot be accessed
我正在使用JQuery.ajax將json字符串發送到php頁面,在該頁面中,我嘗試拉數組並通過變量進行中介。 出於某種原因,運行腳本時我可以在firebug中看到POST,但是當我嘗試獲取參數時,我得到的只是一個空Array()。 我一直在努力弄清楚這個問題,而且還在不知所措。 我從asp.net遷移到.php,所以語法還是有點新鮮,但這似乎應該很簡單。
這是我的jQuery發送代碼。
$('#form_add').submit(function() {
var serial_data = $(this).serialize();
$(".page_content").fadeOut("slow", function(){
$.get(uri_base +'/'+uri_cont+'/get_db_name/true/'+Math.random(), function(data) {
$.ajax({
type: 'POST',
url: uri_base +'/AJAX/add_record/'+data+'/'+Math.random(),
data: serial_data,
contentType: "application/json",
success: function(data){
$.ajax({
type: 'POST',
url: uri_base +"/"+ uri_cont +"/"+ uri_func,
data: data,
success: function(data){
alert(data)
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
});
});
return false;
});
忽略第一個.get(),后者只是拉回變量以完成下一個URL。 first.ajax()調用將序列化的表單數據發送到我的AJAX控制器,並推入數據庫。 控制器的返回發送回我轉換為json的對象數組。
if(isset($key)){
$data['add_msg'] = $this->model_ajax->add_record($table,$array);
$exploded_data = json_encode($data['add_msg']);
print_r($exploded_data);
exit;
}
從那里,下一個.ajax()應該將數據發送到我的控制器,以拉回視圖以顯示在.pag_content div中。 我已經嘗試過回顯json對象,每個循環都有一個用於拉出鍵的方法,還有無數其他方法。 什么都沒有……我收到JSON無效錯誤(嘗試對其進行解碼時),僅輸出“ Array()”,或者在遍歷它們時一無所獲。
這是我在螢火蟲中看到的控制器代碼和POST輸出...
echo $_POST;
$array = $_POST;
echo $array;
foreach($_POST as $key){
echo $key;
}
exit;
[{"id":"29","datetime":"2011-03-23 12:10:25","full_name":"Leroy Brown","email_address":"test@testing.com","password":"asdf","id_group":"0","id_sites":"0","active":"0"}]
在php中輸出任何內容之前,請確保發送正確的content-type。 諸如JQuery和mootools之類的框架傾向於使用Accept字符集進行過濾(應該如此)(您可以在firebug中看到它)
header(“ Content-Type:application / json”); //也可以是text / json,根據您的框架檢查firebug中的accept標頭
:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.