问题
I am working with grails 2.1.1. I have a remote function from where I want to pass more than one java script variable through g:remoteFunction. But no js variable is going to controller. My js variables are underlined and are not recognized. Can anyone please help me on this please ?!!! Here are my attempts below ::
My function >>>
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')}
}
}
回答1:
As remoteFunction finally renders a jQuery function, you can use the params option, to include your variables using jQuery selectors or the Js vars or functions you need.
Here is an example. As you can see, the params value is just an string, but after renderer, it will be run cause after that is js ;)
<button type="button" onclick="${remoteFunction(controller: 'category', action: 'save', update: "eventsDiv",
params: '\'type=\' + myJSvar)}">
${message(code: 'default.button.save.label')}
</button>
回答2:
I believe the code is written in GSP which get complied and rendered as html so it cannot read the user changes on page. For example the element #aaOrgIdAaOrg is a drop down and you want to read the changed value of dropdown then compiled value of GSP tag cannot read this.
If the value of event doesn't change with user events and passed from action to in model then you can directly use the parameter in remote function.
In your case I think if will be better if your write your own ajax code rather then using remoteFunction. RemoteFunction tag is for basic usages and it is also deprecated in newer releases of Grails so if you use this tag you will need to remove it when you decide to migrate to newer version of Grails.
Hope it helps
回答3:
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')}
}
}
回答4:
You should be sending this to an api endpoint on the backend.
来源:https://stackoverflow.com/questions/34389908/how-can-i-pass-javascript-variable-to-grails-controller-using-remotefunction