0

我试图在用户输入一些数据后显示一条警告消息,表示祝贺。问题是我验证了控制器中的输入。如果有错误,我将适当的变量传递给我的视图以突出显示不正确的字段,但我不知道如何显示祝贺警报框。这是我尝试过的,但它不起作用。有什么帮助吗?谢谢!

//In my ImplementNewPixel.gsp
<script>
         $.ajax({
                success:function(result){
                    if(result.message != null && result.message != ""){
                        alert(result.message);
                    }
                }
            });      
</script>

//In my actionsController:
def validate = {

    String message = ""

    if(Info is not valid){
       //return appropriate info to highlight incorrect textfields
    }

    else{
       message = "Congratulations your tracking pixel has been created and added!" as JSON
    }

    return [message: message, OtherStuff: OtherStuffThatIPassToMyGSP]
}

这不是我的确切代码。我只包括了涉及这个问题的主要内容。

4

2 回答 2

1

只需在控制器操作的模型中返回您的消息变量(参见http://grails.org/doc/latest/guide/theWebLayer.html#modelsAndViews),例如:

return [message : message]

...并在您的 gsp 中使用 ${message} 来检索其值,如下所示:

alert("${message}")
于 2013-08-16T21:49:30.837 回答
0

Since you're using ajax, you need to return the model in something your javascript library understands. You appear to be using jQuery which will automatically determine the data type by the response's content-type.

class MyController {
    def validate() {
        return [message: "hello there"] as JSON
    }
}
于 2013-08-16T22:20:55.703 回答