我目前正在使用fullCalendar(jQuery插件),现在仍然陷入愚蠢的问题,但我无法克服它。将json从Action类中转换为ajax
尝试在日历页面加载时从数据库填充事件详细信息。这里是我的代码,
$('#calendar').fullCalendar({
defaultDate: '2016-03-12',
editable: true,
eventLimit: true, // allow "more" link when too many events
eventSources: [
getEvents()
]
});
function getEvents(){
alert("Inside get Events");
$.ajax({
url : "/FullCal/eventDetails.do",
dataType : 'json',
success : function(data){
alert(data)
}
});
}
正在使用Struts框架,这里是我的动作映射,
<action path="/eventDetails" type="com.struts.action.CalendarInviteAction" >
<forward name="success" path="/Calendar.jsp" />
</action>
我的动作类有JSON格式的事件细节数据。我的问题是如何将json数据写入ajax响应(简单来说,如何将json数据返回给ajax调用)。这里是我的动作类修订
public ActionForward execute(ActionMapping mapping , ActionForm form , HttpServletRequest request , HttpServletResponse response) throws Exception{
System.out.println("TEsting inside getEventDetails");
ArrayList<EventForm> arr = new ArrayList<EventForm>();
arr.add(new EventForm("HandOff Meeting" , new Date(), new Date(),"Meeting at HandOffBridge"));
Gson gson = new Gson();
String jsonString = gson.toJson(arr);
response.getWriter().write(jsonString);
return null;
}
jsonString包含JSON格式eventDetails,怎么这回阿贾克斯。
请帮我实现这一点。
感谢您的回应Kishnan。我已在帖子中更新了我的操作类代码。我想将jsonString返回给ajax。请看看并帮助我。 – Krupa
我已添加新的答案,请尝试语法代码。,谢谢 –
感谢您的回复。我想从Action类返回字符串到ajax调用。基于我的理解,如果我使用的数据,你刚才提到它只是添加到ajax请求。如果错误请更正 – Krupa