javascript - Grails:Ajax 参数未到达 Controller

标签 javascript jquery ajax grails

我的 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/

相关文章:

javascript - 在动态加载的表单中激活 jquery ui 小部件

javascript - jquery 选择器全部必须匹配

jquery - Spring MVC JQuery 的问题

javascript - IE11 FormData AJAX Post with File 在 401 后挂起

javascript - 错误: Cannot call method 'createChild' of undefined

javascript - 下划线映射行为

javascript - 如何从日期/时间戳中减去两个不同的日期?

javascript - 如何编写 While 循环来提示一个单词并退出另一个单词的循环

php mail onclick on link执行mysql命令

javascript - Ajax 响应后无法重新触发 CSS 3 动画