2015-10-21 349 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) { 
     } 
    });     
}