JSONを使用する必要があるプロジェクトに取り組んでいます。Struts2、JSON、jQuery を使用しています。次のように httprequest をサーバーに送信しています:-
function getBBSJSONData(ths)
{
//ths is nothing but this pointer
var id=ths.id;
var url = 'bbsJsonAction?actionId='+id;
var myAjax = new Ajax.Request(
url,
{
method: 'post',
onComplete: fetchBBSSuccessData
});
}
今、このリクエストを struts.xml で次のようにマッピングしています
<action name="bbsJsonAction" class="com.x.x.x.BBS_JSON" method="getBBS_json">
<result type="json"/>
</action>
今私のアクションクラスで、私はこのようにJSONオブジェクトを設定しています
public String getBBS_json()
{
JSONObject jsonObj =null;
Map map = new HashMap();
map.put("actionId", actionId);
map.put("count", count);
jsonObj = new JSONObject(map);
System.out.println(jsonObj);
return SUCCESS;
}
最後に、この JSON の結果を次のようにクライアント側で responseText として取得しています:-
function fetchBBSSuccessData(Request){
var d = Request.responseText.evalJSON();
alert(d); // this shows [object, object]
alert(d.actionId); //this shows undefined
alert(d.countd); //this shows undefined
alert(d.jsonData.count);this also shows undefined
}
未定義と表示される理由、この JSON からデータを取得する方法、考えられる変更を提案してください。