jQuery の getJSON() で、配列を送るのってどうやんだっけ?

JAX-RS (サーバ側)

@Path("d3")
@RequestScoped
public class D3Resource {    
    @GET
    @Path("addArray")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Integer> add(@QueryParam("array[]") List<Integer> srcArray) {
        List<Integer> res = new ArrayList<>();
        for (Integer src : srcArray) {
            res.add(src.intValue() + 1);
        }
        return res;
    }
}

jQuery (クライアント側)

<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Hello World!</h1>
        <input type="text" id="t1" value="1"/>
        <input type="text" id="t2" value="2"/>
        <input type="text" id="t3" value="3"/>
        <button>Array</button>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('button').on('click', function(){
                    var ary = new Array();
                    ary.push($('#t1').val());
                    ary.push($('#t2').val());
                    ary.push($('#t3').val());
                    
                    var args = {
                        'array' : ary
                    };
                    
                    var url = 'webresources/d3/addArray';
                    
                    $.getJSON(url, args, function(data){
                        console.log(data);
                        alert(data);
                    })
                });
            });
        </script>
    </body>    
</html>

実行結果


Java#Glassfish


添付ファイル: filearray2.png 2389件 [詳細] filearray1.png 2394件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-10-21 (月) 23:13:13 (3833d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=85c4cc50d8
ISBN10
ISBN13
9784061426061