简体   繁体   English

从GSP到控制器的Grails参数为空

[英]Grails params from GSP to controller are null

I have this button: 我有这个按钮:

<button id = "select"
            onclick = "<g:remoteFunction controller='customer' action='selectBatch'/>"
            params: '\'batchID=\' + selectedBatchID'>Select</button>

which does call this method in my controller: 确实在我的控制器中调用此方法:

def selectBatch = {
    println "Batch Selected: " + params.batchID
}

but with this output: 但是输出如下:

Batch Selected: null

Here is the related javascript that generates and prepares the variable to be passed in the params: 这是相关的javascript,它生成并准备要在params中传递的变量:

var table = document.getElementById('batchTble'),
rows = table.getElementsByTagName('tr');
var selectedBatchID;

for (var i=0,len=rows.length; i<len; i++){
rows[i].onclick = function(){
    console.log(this.cells[0].innerHTML);
    selectedBatchID = parseInt(this.cells[0].innerHTML);
    console.log(selectedBatchID)
    /* if you know it's going to be numeric:
    console.log(parseInt(this.innerHTML),10);
    */
}

I did confirm that the javascript variable "selectedBatchID" does print to the console so it does indeed hold a value. 我确实确认javascript变量“ selectedBatchID”确实会打印到控制台,因此确实包含一个值。 But the value never makes it into the params that the controller gets for some reason. 但是,该值永远不会由于某种原因而成为控制器获取的参数。 Here is what the Chrome debug console give me at the same time as the controller complains about null: 这是Chrome调试控制台在控制器抱怨null的同时给我的:

17 VM3216:53
POST http://ec2-54-209-146-139.compute-1.amazonaws.com:8080/FatcaOne_0/customer/selectBatch 404 (Not Found) jquery.tools.min.js:38
send jquery.tools.min.js:38
onclick

You can see that the value '17' printed in the Chrome debug console but never makes it into the controller as params.batchID. 您会看到在Chrome调试控制台中打印了值“ 17”,但从未将其作为params.batchID放入控制器中。 Why my param is null and I get the 404 not found is what I do not understand? 为什么我的参数为null却找不到404,这是我不理解的? Any help appreciated. 任何帮助表示赞赏。 I'd be happy to provide more details on request. 我很乐意根据要求提供更多详细信息。

onclick looks incorrect. onclick看起来不正确。 Refer docs for details. 有关详细信息,请参阅文档 Based on that it should be something like: 基于此,它应该类似于:

<button id = "select"
        onclick = "${remoteFunction(controller: 'customer', 
                                    action: 'selectBatch',
                                    params: '\'batchID=\' + selectedBatchID')}">
Select
</button>

I suppose you have mixed the taglib and method call syntax as shown in the question (usage of key-value for params versus attributes for action and controller). 我想您已经混合了问题中显示的taglib和方法调用语法(参数的键值用法与操作和控制器的属性)。 Moreover, params is not part of remoteFunction altogether in the question. 而且,问题中params完全不是remoteFunction一部分。

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

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