如何使用remoteFunction将JavaScript变量传递给Grails控制器

问题描述 投票:0回答:4

我正在使用 Grails 2.1.1。我有一个远程函数,我想通过 g:remoteFunction 传递多个 JavaScript 变量。但是 JS 变量不会传递给控制器。我的 JS 变量有下划线,无法识别。

这是我的尝试:

function addNewBill(){
    var orgId = $('#aaOrgIdAaOrg').val();
    var ccId = $('#costCenter').val();
    if(orgId != null && orgId != "null" && orgId != "" && ccId != null && ccId != "null" && ccId != ""){
        $('#ccModal').modal('hide');
        ${remoteFunction(action: 'createBill', params: [purchaseOrderId: purPoMstInstance.id, aaOrgIdAaOrg: orgId, costCenter: ccId], update: 'bill')}            
    }
}

接下来我可以尝试什么?

javascript grails grails-2.0 parameters
4个回答
1
投票

当remoteFunction最终呈现一个jQuery函数时,您可以使用params选项,使用jQuery选择器或您需要的Js变量或函数来包含您的变量。

这是一个例子。正如你所看到的,params 值只是一个字符串,但是在渲染器之后,它将被运行,因为之后是 js ;)

<button type="button" onclick="${remoteFunction(controller: 'category', action: 'save', update: "eventsDiv",
   params: '\'type=\' + myJSvar)}">
         ${message(code: 'default.button.save.label')}
</button>

0
投票

我相信代码是用 GSP 编写的,它被编译并呈现为 html,因此它无法读取页面上的用户更改。例如,元素 #aaOrgIdAaOrg 是一个下拉列表,您想要读取下拉列表的更改值,然后 GSP 标记的编译值无法读取此值。

如果事件的值不随用户事件而改变,并且从动作传递到模型中,那么您可以直接使用远程函数中的参数。

就你而言,我认为如果你编写自己的ajax代码而不是使用remoteFunction会更好。 RemoteFunction 标签用于基本用途,在较新版本的 Grails 中也已弃用,因此如果您使用此标签,则在决定迁移到较新版本的 Grails 时需要将其删除。

希望有帮助


0
投票
function addNewBill(){
var orgId = $('#aaOrgIdAaOrg').val();
var ccId = $('#costCenter').val();
if(orgId != null && orgId != "null" && orgId != "" && ccId != null && ccId != "null" && ccId != ""){
    $('#ccModal').modal('hide');
    ${remoteFunction(action: 'createBill', params: '\'orgId=\' + orgId+\'&ccId=\' + ccId', update: 'bill')}            
}

}


-1
投票

您应该将其发送到后端的 api 端点。

© www.soinside.com 2019 - 2024. All rights reserved.