2012-08-24 69 views
0

我是JSON的新手,並不熟悉jQuery。

我一直試圖讓$ .getJSON函數現在工作幾個小時,而不是。這裏是我的設置:

ajax.php文件:

<?php 
require_once('../../libs/connection.class.php'); 
require_once('../../libs/actions.class.php'); 

$dbcon = new connection(); 

$actions = new action($dbcon); 

if (isset($_GET['action'])) { 

switch ($_GET['action']) { 

    case 'getstates': 
     header('Content-Type: application/json'); 
     echo json_encode($actions->liststates()); 
     break; 

    default: 
     break; 
} 

} 
?> 

scripts.js中的文件:

$('select[name=stationcountry]').change(function(){ 

    var value = $(this).val(); 

    $.getJSON('lib/ajax.php?action=getstates',function(data){ 

     //What CODE TO PUT HERE? 

     $("#kirky").html() 
    }; 



    }); 



}); 

的actions.class.php - 這是liststates類:

public function liststates(){ 

    $states = array(
     'AL'=>"Alabama", 
     'AK'=>"Alaska", 
     'AZ'=>"Arizona", 
     'AR'=>"Arkansas", 
     'CA'=>"California", 
     'CO'=>"Colorado", 
     'CT'=>"Connecticut", 
     'DE'=>"Delaware", 
     'DC'=>"District Of Columbia", 
     'FL'=>"Florida", 
     'GA'=>"Georgia", 
     'HI'=>"Hawaii", 
     'ID'=>"Idaho", 
     'IL'=>"Illinois", 
     'IN'=>"Indiana", 
     'IA'=>"Iowa", 
     'KS'=>"Kansas", 
     'KY'=>"Kentucky", 
     'LA'=>"Louisiana", 
     'ME'=>"Maine", 
     'MD'=>"Maryland", 
     'MA'=>"Massachusetts", 
     'MI'=>"Michigan", 
     'MN'=>"Minnesota", 
     'MS'=>"Mississippi", 
     'MO'=>"Missouri", 
     'MT'=>"Montana", 
     'NE'=>"Nebraska", 
     'NV'=>"Nevada", 
     'NH'=>"New Hampshire", 
     'NJ'=>"New Jersey", 
     'NM'=>"New Mexico", 
     'NY'=>"New York", 
     'NC'=>"North Carolina", 
     'ND'=>"North Dakota", 
     'OH'=>"Ohio", 
     'OK'=>"Oklahoma", 
     'OR'=>"Oregon", 
     'PA'=>"Pennsylvania", 
     'RI'=>"Rhode Island", 
     'SC'=>"South Carolina", 
     'SD'=>"South Dakota", 
     'TN'=>"Tennessee", 
     'TX'=>"Texas", 
     'UT'=>"Utah", 
     'VT'=>"Vermont", 
     'VA'=>"Virginia", 
     'WA'=>"Washington", 
     'WV'=>"West Virginia", 
     'WI'=>"Wisconsin", 
     'WY'=>"Wyoming" 
    ); 

    return $states; 


    } 

這裏是頁面輸出的JSON:

{"AL":"Alabama","AK":"Alaska","AZ":"Arizona","AR":"Arkansas","CA":"California","CO":"Colorado","CT":"Connecticut","DE":"Delaware","DC":"District Of Columbia","FL":"Florida","GA":"Georgia","HI":"Hawaii","ID":"Idaho","IL":"Illinois","IN":"Indiana","IA":"Iowa","KS":"Kansas","KY":"Kentucky","LA":"Louisiana","ME":"Maine","MD":"Maryland","MA":"Massachusetts","MI":"Michigan","MN":"Minnesota","MS":"Mississippi","MO":"Missouri","MT":"Montana","NE":"Nebraska","NV":"Nevada","NH":"New Hampshire","NJ":"New Jersey","NM":"New Mexico","NY":"New York","NC":"North Carolina","ND":"North Dakota","OH":"Ohio","OK":"Oklahoma","OR":"Oregon","PA":"Pennsylvania","RI":"Rhode Island","SC":"South Carolina","SD":"South Dakota","TN":"Tennessee","TX":"Texas","UT":"Utah","VT":"Vermont","VA":"Virginia","WA":"Washington","WV":"West Virginia","WI":"Wisconsin","WY":"Wyoming"} 

有人能幫我列出JSON輸出的所有狀態嗎?

謝謝。

+0

所以你在做一個AJAX' GET'調用,期待json數據被返回? – Matt

+0

'$ .getJSON'不工作,或者您需要顯示返回對象的幫助嗎?或兩者? – MrOBrian

回答

4

假設JSON是正確的檢索和jQuery的處理,那麼它只是另一個JavaScript數據結構,而你遍歷它來建立自己的國家名單,如:

$.each(data, function(key, val) { 
    $('#kirky').append(key + ': ' + val + '<br />'); 
}); 
+0

謝謝,這工作完美。 –