简体   繁体   English

在Play框架中从视图向控制器进行ajax调用

[英]ajax call from view to controller in Play framework

I am newbie to MVC and Play framework (Java). 我是MVC和Play框架(Java)的新手。 Instead of using Groovy for dynamic HTML, I made our own page with static HTML, I mean we haven't any Groovy expressions. 我没有使用Groovy生成动态HTML,而是使用静态HTML创建了自己的页面,这意味着我们没有任何Groovy表达式。 Here, I have a controller "Customer", generates JSON object which has to be sent to an ajax call in view. 在这里,我有一个控制器“ Customer”,生成了JSON对象,该对象必须发送到视图中的ajax调用。 I tried with render() method, seems I haven't used correctly. 我尝试了render()方法,似乎我使用不正确。 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调用视图“ 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: 我以前从未使用过rootName,我通常只是这样做:

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. 旁注:使用refreshNotifications时,我运行了一个Security方法,该方法将验证并填充用户对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将参数从视图传递到Play框架上的控制器 - Passing parameter from view to controller on Play Framework Ajax 调用将数据从视图传递到控制器 - Ajax Call to pass the data from view to Controller 在Play框架Java中将视图的复选框列表发送到控制器 - sending a list of checked boxes from view to controller in play framework java 无法通过Ajax渲染的局部视图调用控制器操作 - Unable to call controller action from a partial view rendered via ajax 视图中的呼叫控制器 - call controller from a view 如何从 ajax 调用端点,它从 controller 重定向到另一个包含数据的视图? - How to call endpoint from ajax where it redirects from controller to another view with data? 如何使用 ajax 调用或任何其他方法将 arrays 从视图传递到 controller? - How to pass arrays from view to controller using ajax call or any other method? 将@Model 从视图发送到 controller 和 ajax - send @Model from view to controller with ajax 使用 Ajax 将数据从视图传递到控制器 - Pass data from View to Controller using Ajax 如何从Zend Framework中的索引控制器调用自定义控制器 - How to call custom controller from index controller in Zend Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM