0

我需要进行 Ajax 查询来调用 Service 对象中的一个方法,该方法接受 3 个参数并返回一个布尔值。然后,我将这个布尔值用于在发布前发生的验证消息。

这就是我目前所拥有的(不工作),但我尝试了其他事情但无济于事。我们正在使用 JQuery 和 Grails:

var isUnique = ${remoteFunction(
    service: 'Project', 
    action:'checkUniqueUserProjectId',
    params:{
        // These values are from  hidden fields in the form.
        // userId and projectId are string values and the group is an object
        uniqueId: userId,
        group: userGroup,
        projectId: myProjectId
    }
)}

这是在 ProjectService 中调用的方法:

// Check whether or not a Project with the provided uniqueId already exists 
// in the database that is not itself.
def checkUniqueUserProjectId(uniqueId,group,projectId) {
    def filterCriteria = Project.createCriteria()
    def projectList = filterCriteria.list {
        and {
            eq("userProjectId", uniqueId)
            eq("group", group)
            ne("id",projectId)
        }
    }

    if(projectList.empty)
        return true
    else
        return false
}

任何帮助将不胜感激!

4

3 回答 3

0

I ended up using a remote function with the code as follows:

${remoteFunction(action: 'isUniqueId', params: '\'uniqueid=\'+userId+\'&project_id=\'+myProjectId', onFailure:'$("#project").submit()', onSuccess:'verifyUniqueness(data, textStatus,detailsMessage)') }

....

function verifyUniqueness(data, textStatus,detailsMessage) {
    if (data == 'false') {
        detailsMessage = detailsMessage + "A Project with the provided Project ID already exists.";
    }

    if (detailsMessage.trim() != '' ) { 
        stackedSummary.section('details', '<font style="color:red; font-weight:normal; line-height:0px;">' + detailsMessage + '</font>');
    } else {
        $('#project').submit();
    }
}

ProjectController

def isUniqueId = {
    def project = Project.get(params.project_id)
    if (project == null) {
        render(projectService.checkUniqueUserProjectId(params.uniqueid, null, null)) as JSON
    } else {
        render(projectService.checkUniqueUserProjectId(params.uniqueid, project.group, project.id)) as JSON
    }
}

Thank you everyone for your help! You helped lead me in the right direction :)

于 2013-01-07T17:54:20.390 回答
0
$.post('Project/checkUniqueUserProjectId', {
        uniqueId : userId,
        group    : userGroup,
        projectId: myProjectId
}, 
function (response) {
    if (response.firstChild.textContent=="true") {
        alert("hola mundo");
    }
});

如果响应是 JSON 格式会更好

于 2013-01-04T18:27:55.400 回答
0

所以在你的控制器中:

if ( projectService.checkUniqueUserProjectId(params.uniqueId, params.group, params.projectId) )
{
    render([:] as JSON)
}
else
{
    response.status = 403
}

JavaScript:

var isUnique;

$.ajax({
    type: "POST",
    url: "contextPath/controller/action",
    data: {uniqueId: userId, group: userGroup, projectId: myProjectId},
    success: function() { isUnique = true; },
    error: function() { isUnique = false; },
    dataType: "json"
});
于 2013-01-04T18:48:04.937 回答