DEV

JQueryで県データ取得

HeartRails Expressで駅データをAjaxで取得・表示したい。まずは、県データを取得・表示するコード。

<h3>駅データの取得</h3>
<script type="text/javascript">
$(function(){
$(window).load(function () {get_ken();});
});
function get_ken() {
$.ajax({
dataType: "jsonp",
data: {"method":'getPrefectures'},
url: "http://express.heartrails.com/api/json",
success: function (data) {
$.each(data.response.prefecture, function(idx,ken_name){
$("<option/>")
.append(ken_name)
.appendTo("#station");
});
}
});
}
</script>
<select id="station"></select>