搜尋此網誌

2013年1月16日 星期三

JQuery getJson 用法

getJsonLoad.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-1.8.3.min.js"></script>
<script type="text/javascript">
function getFruits() {

    $.getJSON('../jsp/load_json.jsp', function(data) {

        $.each(data, function(key, val) {
            $("#fruitsBox").append('<option id="' + key + '">' + val + '</option>');
        });

    });
}
</script>
</head>
<body>
<select id = "fruitsBox"></select>

<button onclick ="getFruits()">get fruits</button> 
</body>
</html>
load_json.jsp
<%@ page contentType="text/html" import="org.json.* , java.io.*"%>
<%

    JSONObject obj = new JSONObject();
    obj.put("1", "apple");
    obj.put("2", "orange");
    obj.put("3", "pineapple");
    
    PrintWriter pw = response.getWriter();
    pw.print(obj.toString());
    
%>

Reference:JQuery getJson

沒有留言:

張貼留言