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()
    };



    });



});

アクション.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出力からすべての状態をリストするのを手伝ってもらえますか?

ありがとう。

4

1 に答える 1

4

jsonが適切に取得され、jqueryによって処理されると仮定すると、それは単なる別のjavascriptデータ構造であり、それをループして状態リストを作成します。例:

$.each(data, function(key, val) {
    $('#kirky').append(key + ': ' + val + '<br />');
});
于 2012-08-24T19:32:10.297 に答える