[英]ajax call from view to controller in Play framework
我是MVC和Play框架(Java)的新手。 我没有使用Groovy生成动态HTML,而是使用静态HTML创建了自己的页面,这意味着我们没有任何Groovy表达式。 在这里,我有一个控制器“ Customer”,生成了JSON对象,该对象必须发送到视图中的ajax调用。 我尝试了render()方法,似乎我使用不正确。 你能给我一些想法从这里转发吗? 谢谢。
public static void customer(){
WordAPI objWordAPI=new WordAPI();
List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId);
JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words");
render("Application/wordcloud.html",modelSerializer.serialize(listObjWord));
}
和ajax调用视图“ wordcloud.html”
$.ajax({
url: "/customer",
dataType : 'json',
success: function (data) {
alert(data);
}
})
我相信这应该有效:
public static void customer(){
WordAPI objWordAPI=new WordAPI();
List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId);
JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words");
renderJSON(modelSerializer.serialize(listObjWord));
}
我以前从未使用过rootName,我通常只是这样做:
public static void refreshNotifications()
{
JSONSerializer notifySerializer = new JSONSerializer().include("message","notifyId","class").exclude("*");
List<Notification> notificationList = user.getNotifications();
renderJSON(notifySerializer.serialize(notificationList));
}
旁注:使用refreshNotifications时,我运行了一个Security方法,该方法将验证并填充用户对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.