[英]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.