简体   繁体   中英

ajax call from view to controller in Play framework

I am newbie to MVC and Play framework (Java). Instead of using Groovy for dynamic HTML, I made our own page with static HTML, I mean we haven't any Groovy expressions. Here, I have a controller "Customer", generates JSON object which has to be sent to an ajax call in view. I tried with render() method, seems I haven't used correctly. can you give me some idea to forward from here. thanks.

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));
  }

and ajax call in view "wordcloud.html"

$.ajax({
    url: "/customer",
    dataType : 'json',
    success: function (data) {
        alert(data);
             }
        })

I believe this should work:

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));
  }

I've never used rootName before, I usually just do something more like this:

public static void refreshNotifications()
    {
        JSONSerializer notifySerializer = new JSONSerializer().include("message","notifyId","class").exclude("*");
        List<Notification> notificationList = user.getNotifications();
        renderJSON(notifySerializer.serialize(notificationList));
    }

Side Note: With refreshNotifications I have a Security method I run before which verifies and populates the user object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM