2011-05-18 28 views
0

可以做出類似這樣的事情嗎?關於加載函數和json的問題

$.ajax({ 
    url: "test.php", 
    success: function(json, json1){ //Question here, can i have more than one? 
    $m0 = []; 
    $m0.push(parseFloat(json)); 
    alert($m0); //show 750 

    $m1 = []; 
    $m1.push(parseFloat(json1)); 
    alert($m1); // show 320 
    } 
}); 

什麼是預期在json中返回?

這個例子? [750, 320]或此[750] [320]?

回答

2

我不認爲這是可能的; JSON通常只包含一個頂級值。標準的做法是有test.php返回兩個值的JSON數組:然後

[750, 320] 

你的負載功能應該是這樣的:

$.ajax({ 
    url: "test.php", 
    success: function(json){ 
    $m0 = []; 
    $m0.push(parseFloat(json[0])); 

    $m1 = []; 
    $m1.push(parseFloat(json[1])); 
    }, 
    // this will ask jQuery to parse the JSON for you; 
    // otherwise, your success function will receive the 
    // string "[750, 320]" as the argument 
    dataType: 'json' 
}); 
+0

但對我來說JSON的值代表兩個查詢的。所以,我做的是:echo json_encode($ result0);和echo json_encode($ result1);我怎麼可以在不同的查詢的相同的數組值進行編碼? – 2011-05-18 23:09:41

+2

你的意思是兩個數據庫查詢?如果你想發送的數據在'$ result0'和'$ result1'中,那麼你可以將它們包裝在一個數組中並回顯:'echo json_encode(array($ result0,$ result1));' – nrabinowitz 2011-05-18 23:18:46

+0

yes, , 謝謝 – 2011-05-18 23:21:22