简体   繁体   English

$ .getJSON函数不适用于php json_encoded数组

[英]$.getJSON function not working with php json_encoded array

I'm new to JSON and not that familiar with jQuery. 我是JSON的新手,对jQuery并不熟悉。

I have been trying to get the $.getJSON function to work for hours now, and it won't. 我一直在尝试使$ .getJSON函数现在可以工作几个小时,但实际上不会。 Here's my setup: 这是我的设置:

ajax.php file: 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 file: 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 - this is the liststates class: 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;


    }

and here is the JSON that the page outputs: 这是页面输出的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"}

can someone please help me list all the states from the JSON output? 有人可以帮我列出JSON输出中的所有状态吗?

Thanks. 谢谢。

Assuming the json is retrieved properly and processed by jquery, then it's just another javascript data structure, and you loop over it to build your states list, eg: 假设json已正确检索并由jquery处理,则它只是另一个javascript数据结构,您可以对其进行循环以构建状态列表,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM