$ .getJSON函数不能与php json_encoded数组一起使用

时间:2012-08-24 19:26:54

标签: php jquery json

我是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输出中的所有状态吗?

感谢。

1 个答案:

答案 0 :(得分:4)

假设json被正确检索并由jquery处理,那么它只是另一个javascript数据结构,你循环它以构建你的状态列表,例如:

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