我的 GSP 中有一个按钮,如下所示:
<button id = "select"
onclick = "${remoteFunction(controller: 'customer',
action: 'selectBatch',
params: '\'batchID=\' + selectedBatchID')}">
Select
</button>
一切都很好,“batchID”参数很好地到达了我的 Controller ,但我意识到我不想每次单击按钮时都调用此函数,只有在满足某些条件时才调用此函数。所以,我像这样重新定义了我的按钮:
<button id = "select" >Select</button>
并将其绑定(bind)到一个 JavaScript 函数,如下所示:
$('#select').click(function () {
if (selectedBatchID == undefined) {
$("#errorMessages p").text("Click a row to choose a batch first.");
$("#errorMessages p").show();
$("#errorMessages p").fadeOut(3000);
}
else {
validBatchID = selectedBatchID;
$("#choosebatch h2").text("Batch ID " + selectedBatchID + ": " + selectedBatchDesc);
$('#AdminConsole p').css("font-size", "30px");
$("#AdminConsole p").html("Batch ID " + selectedBatchID + "<br />" + selectedBatchDesc);
console.log(selectedBatchID);
$.ajax({
type: 'POST',
url: "${createLink(action:'selectBatch', controller:'customer')}/batchID=" + selectedBatchID
});
}
});
现在看来这个变量没有达到我的 Controller 的功能,如下所示:
def selectBatch = {
println "Batch ID: " + params.batchID
selectedBatch = Batch.findWhere(id: params.batchID.toInteger())
println "Batch selected: " + selectedBatch
}
因为我的 Controller 提示:
............Batch ID: null
Error |
2014-06-10 16:27:54,686 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [POST] /FatcaOne_0/customer/selectBatch /batchID=35
Cannot invoke method toInteger() on null object. Stacktrace follows:
Message: Cannot invoke method toInteger() on null object
这是 Chrome 调试器显示的内容:
35 VM725:78
POST http://hostname/FatcaOne_0/customer/selectBatch/batchID=35 500 (Internal Server Error) jquery.tools.min.js:38
send jquery.tools.min.js:38
f.extend.ajax jquery.tools.min.js:38
(anonymous function) VM725:79
f.event.dispatch jquery.tools.min.js:37
h.handle.i
所以我们可以看到它不为 null,因为它在 Chrome 调试器中打印了值“35”,但是为什么 Controller 没有看到这一点,特别是当它也包含在 Ajax 形成的 URI 中时打电话?
最佳答案
尝试用以下内容替换您的$.ajax
:
$.ajax({
url: "${createLink(controller: 'customer', action: 'selectBatch')}",
type: 'POST',
data: {batchID: selectedBatchID}
});
关于javascript - Grails:Ajax 参数未到达 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150471/