Struts1にてajaxの機能を実装しようとしております。
普通の文字列を送る機能は実装できたのですが、アクションで生成したBeanをajaxで送るということができません。
下記のコードだと、ajax側でデータを取得できない状態です。
(obj3のaddをコメント化すればデータを取得できます。)
また、Bean内にもしリストがあった場合にそれもクライアント側で表示できるか知りたいです。
JSONObjectはjson-simple-1.1jarを使用しています。
方法をご教授していただければ幸いです。
・Javascript
$(function() { $("#search_button").click(function(){ $.getJSON( "/Server/Ajax.do", function(result){ alert(111); for(var i=0; i<result.length; i++){ if(i==2){ alert(result[i].formBean.ID); continue; } $("<OPTION></OPTION>").val(i).text(result[i].name + "(" + result[i].age + ")").appendTo("#users"); } result.length; } ); }); });
・アクション
HensyuForm hensyuForm = new HensyuForm(); hensyuForm.setID("IDTEST"); JSONObject obj1 = new JSONObject(); obj1.put("name", "太朗"); obj1.put("age", 27); JSONObject obj2 = new JSONObject(); obj2.put("name", "花子"); obj2.put("age", 25); JSONObject obj3 = new JSONObject(); obj3.put("formBean", hensyuForm); JSONArray json = new JSONArray(); json.add(obj1); json.add(obj2); json.add(obj3); PrintWriter writer; System.out.println(json.toString()); response.setCharacterEncoding("UTF-8"); writer = response.getWriter(); writer.print(json.toString()); writer.close(); return null;
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。