我是 ajax 调用的新手,并试图让一个简单的示例工作,以填充国家下拉列表。我已经验证我正在从通话中取回数据,但在实际填充下拉列表时遇到了麻烦。
这是html:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
function loadlist(selobj,url,nameattr)
{
$(selobj).empty();
$.getJSON(url,{},function(data)
{
$.each(data, function(i,obj)
{
$(selobj).append($('<option></option>').val(obj[nameattr]).html(obj[nameattr]));
});
});
}
$(function()
{
loadlist($('select#country').get(0), 'http://127.0.0.1/country1.php','country');
});
</script>
</head>
<body>
Country:<select name='country' id='country' size='1'></select>
</body>
</html>
这是 country1.php 文件:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
$output = '{
" " : " ",
"US" : "United States",
"AF" : "Afghanistan",
"AL" : "Albania",
"DZ" : "Algeria",
"AS" : "American Samoa",
"AD" : "Andorra",
"AO" : "Angola",
...
"ZM" : "Zambia",
"ZW" : "Zimbabwe" }';
echo $output;
?>
我需要做什么才能获得正常的下拉菜单?提前谢谢了。