2015-10-21 127 views
0

在一个Grails查看我需要调用JavaScript方法来获取一些信息,所以我有这样的提交动作:Grails的javascript调用控制器方法

<input type="submit" name="submit" class="submit action-button" value="Generar" onclick="generateReport()" style="float: right" /> 

,并在generateReport结束()我需要调用/重定向到一个控制器的动作show(因为我在create动作是已经)

I'have试图与

1) var jSon = generateJSON(); 
    <g:remoteFunction controller="report" action="show" params="[data:jSon]" /> 

2) var jSon = generateJSON(); 
    <g:remoteFunction controller="report" action="show" params="[data:${jSon}]" /> 

1)数据到达空 2)编译错误:

org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException 
Message 
Attribute value quote wasn't closed (controller="report" action="show" params="[data:${jSon}]"). 
+1

你不应该使用'remoteFunction'或任何'远程*'标签。它们很旧,很难合作,并且不再支持新版本的Grails。您需要学习使用jquery/javascript制作自己的ajax调用。 –

回答

0

变种JSON作为PARAMS在不能被分配到数据,作为VAR JSON是JavaScript变量。 params属性中的数据需要来自控制器的模型参数。

第一个选项当它被分配为参数,因为它不是来自控制器的模型参数,它显示为null

第二个选项不起作用,因为这不是从控制器定义模型参数的方式,所以给出编译错误

所以,你应该更好地尝试从控制器获取jSon作为模型,然后定义为参数。

或者你可以点击此链接来定义G:remoteFunction中的onclick本身

http://grails.github.io/grails-doc/2.2.1/ref/Tags/remoteFunction.html

希望这有助于!谢谢。

0

remoteFunction标记将不适用于您的情况,因为它已呈现为html和JavaScript(服务器端)之前 jSon的值已知(客户端)。

你必须自己完成ajax调用(例如Jquery)。

0

你可以试试这个..

在你的GSP声明一个变量这样的..

<script type="text/javascript"> 
      var yourVariable = "${createLink(url: [controller: 'yourController', action: 'yourAction'])}"; 
</script> 
在你的js file..you可以使用Ajax

然后。

例如AJAX

function checkUsernameAvailable(user, example){ 
    $.ajax(
    { 
     url: yourVariable, <- this variable take from your gsp 
     contentType:"text/json", 
     type: "get", 
     data: ({ id: usernamenya}), 
     dataType: "json", 
     cache: false, 
     async: false, 
     success: function(data) { 
       //do something here 
     }, 
     error: function(xhr) { 
     } 
    });     
}