Grails非null params到达控制器为null?

时间:2014-08-09 20:27:34

标签: javascript grails

我在试图通过Javascript点击处理程序将我的GSP中的参数传递给我的控制器时遇到了一些麻烦:

$('#save').click(function () { 
        var uniqueId = "${recordToEdit.uniqueId}";
        var secondaryId = "${recordToEdit.secondaryId}";
        console.log(removedYellowIssues);
        <g:remoteFunction controller="customer" 
                            action="saveModifiedIndividualRecord"
                            params='{uniqueId: uniqueId, 
                                    secondaryId: secondaryId,
                                    yellowIssuesRemoved: removedYellowIssues,
                                    redIssuesRemoved: removedRedIssues}'/>
    });

当按下“保存”按钮时,这是我在javascript控制台中看到的:

["No address provided."] 

所以你可以看到'removedYellowIssues'列表不是空的。这是一个包含一个字符串的Javascript列表。但是,这是我的控制器所想的:

<><><> Parameters ***:
<><><> uniqueId: 239400B
<><><> secondaryId: 1
<><><> Red issues removed: null
<><><> Yellow issues removed: null

以下是控制器操作:

def saveModifiedIndividualRecord() {
        println "<><><> Parameters ***: "
        println "<><><> uniqueId: " + params.uniqueId
        println "<><><> secondaryId: " + params.secondaryId
        println "<><><> Red issues removed: " + params.redIssuesRemoved
        println "<><><> Yellow issues removed: " + params.yellowIssuesRemoved
    }

以下是包含上述保存按钮代码段的更多Javascript代码。

var currentYellowIndex = 0;
    var allYellowIssues = $('#allYellowIssues'); // The unordered list 'ul'
    var removedYellowIssues = []; // An array to keep track of issues removed
    if (allYellowIssues.length) { // If there are issues to be displayed
      var yellowElements = document.getElementsByName('yellowIssue');
      var yellowListSize = yellowElements.length;
      yellowElements[currentYellowIndex].className = "display";
      $('#yellowStartIndex').html(currentYellowIndex + 1);      
      $('#yellowSizeIndex').html(yellowListSize);

      $('#nextYellowIssue').click(function () {
        if (currentYellowIndex < yellowListSize-1) {
            yellowElements[currentYellowIndex++].className = "display-none";
            yellowElements[currentYellowIndex].className = "display";
            $('#yellowStartIndex').html(currentYellowIndex + 1);    
        }
     });

      $('#previousYellowIssue').click(function () {
        if (currentYellowIndex > 0) {
            yellowElements[currentYellowIndex--].className = "display-none";
            yellowElements[currentYellowIndex].className = "display";
            $('#yellowStartIndex').html(currentYellowIndex + 1);    
        }
     });

     $('#clearYellowFlag').click(function () {
        removedYellowIssues.push(yellowElements[currentYellowIndex].innerHTML);
        yellowElements[currentYellowIndex].className = "display-none";
        yellowElements[currentYellowIndex].remove();
        yellowListSize = yellowElements.length;
        if (yellowListSize == 0) 
            $('#yellowIssues').hide();
        else {
            currentYellowIndex = 0;
            yellowElements[currentYellowIndex].className = "display";
            $('#yellowStartIndex').html(currentYellowIndex + 1);      
            $('#yellowSizeIndex').html(yellowListSize);
        }
      });
    }

    $('#save').click(function () { 
        var uniqueId = "${recordToEdit.uniqueId}";
        var secondaryId = "${recordToEdit.secondaryId}";
        console.log(removedYellowIssues);
        <g:remoteFunction controller="customer" 
                            action="saveModifiedIndividualRecord"
                            params='{uniqueId: uniqueId, 
                                    secondaryId: secondaryId,
                                    yellowIssuesRemoved: removedYellowIssues,
                                    redIssuesRemoved: removedRedIssues}'/>
    });

GSP的最后一部分是保存按钮本身定义如下:

<br>
<button id="save"> Save </button>&nbsp&nbsp&nbsp
<button id="cancel" class="close" type="button"> Cancel </button>

1 个答案:

答案 0 :(得分:1)

我觉得params中的{}应该是[]。 g:remoteFunction是GSP代码,参数应该是地图。

<g:remoteFunction controller="customer" 
                            action="saveModifiedIndividualRecord"
                            params='[uniqueId: uniqueId, 
                                    secondaryId: secondaryId,
                                    yellowIssuesRemoved: removedYellowIssues,
                                    redIssuesRemoved: removedRedIssues]'/>

但是,你真的不应该使用那个标签(我认为它在最新版本中已被弃用)。你应该通过jQuery做一个帖子:

$.post("${g.createLink(action: 'saveModifiedIndividualRecord')", {uniqueId: uniqueId, ...}, function(result) {
    ...
});