我有一點問題。我試圖編寫一個Ajax帖子來從數據庫中獲取值,它的返回格式是一個JSON對象。 如何獲取密鑰並從中獲取值對?
jquery的哪個發送AJAX:
功能getTableData(){
$.post('loader.php',getGetStr(),function(data){
var json = $.parseJSON(data);
console.log(json);
});
}
的的console.log輸出是:
Object {query-data: Array[3]}
query-data: Array[3]
0: Object
buy_type: "kiado"
condition_type: "uj"
district: "1"
heat_type: "cirko"
id: "1"
lift_type: "all"
parking_type: "all"
price_max: "22"
price_min: "10"
prop_type: "lakas"
room_max: "3"
room_min: "1"
street: "all"
uid: "3"
__proto__: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
__proto__: Object
PHP代碼至極選擇權的東西,並將數據返回給ajax:
$ordering = array ("buy_type " . $_POST['buyType'],"prop_type ".$_POST['propertyType'],"district ".$_POST['disctrict'],
"street ".$_POST['street'],"room_min ".$_POST['roomMin'],"room_max ".$_POST['roomMax'],
"price_min ".$_POST['priceMin'],"price_max ".$_POST['priceMax'],"condition_type ".$_POST['conditionType'],"heat_type ".$_POST['heatType'],"lift_type ".$_POST['liftType'],"parking_type ".$_POST['parkingType']);
$user=$_SESSION["user"];
$whois = $mysqli->query('SELECT * FROM users WHERE uid='.$mysqli->real_escape_string($user).' ');
$who = $whois->fetch_assoc();
switch($who['user_title']){
case '0':
$res=$mysqli->query('SELECT * FROM searches WHERE uid='.$mysqli->real_escape_string($user).'
ORDER BY '.$mysqli->real_escape_string($ordering[0]).',
'.$mysqli->real_escape_string($ordering[1]).',
'.$mysqli->real_escape_string($ordering[2]).',
'.$mysqli->real_escape_string($ordering[3]).',
'.$mysqli->real_escape_string($ordering[4]).',
'.$mysqli->real_escape_string($ordering[5]).',
'.$mysqli->real_escape_string($ordering[6]).',
'.$mysqli->real_escape_string($ordering[7]).',
'.$mysqli->real_escape_string($ordering[8]).',
'.$mysqli->real_escape_string($ordering[9]).',
'.$mysqli->real_escape_string($ordering[10]).',
'.$mysqli->real_escape_string($ordering[11]).'
') or die($mysqli->error);
while($ki=$res->fetch_assoc()){
$tomb[] = $ki;
}
$tomb = array("query-data"=>$tomb);
echo json_encode($tomb);
任何人都可以幫助我將這些值寫入表中嗎?
而你嘗試過什麼原則? –
這只是一個普通的Javascript對象,你可以用'json ['query-data']'來訪問它。這是一個可以迭代的數組。 – Barmar
只需要一個建議,將'$ .post'的額外參數作爲'json'傳遞給你,所以你不需要使用'$ .parseJSON'。 ':即:$ .post(url,params,callback,'json')'; – bystwn22