gpt4 book ai didi

javascript - 从 Action 类将 json 返回到 ajax

转载 作者:行者123 更新时间:2023-12-02 14:55:41 24 4
gpt4 key购买 nike

我目前正在开发 fullCalendar(jQuery 插件),现在遇到了愚蠢的问题,但我无法克服它。

尝试在日历页面加载时从数据库填充事件详细信息。这是我的代码,

$('#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调用)。这是我的 Action 类更新

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,如何将其返回给ajax。

请帮助我实现这一目标。

最佳答案

你的ajax调用看起来没问题。只需要以正确的方式显示ajax响应即可。

events: function(start, end, timezone, callback) {
$.ajax({
url : "/FullCal/eventDetails.do",
dataType : 'json',
success : function(data){
callback(data);
}
});
}

关于javascript - 从 Action 类将 json 返回到 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com