Redirecting from one controller action to another controller action from ajax call
I am new to grails and I am stuck with the following problem.
I am calling an action1 in controller1 through an ajax call. Now though it I want to redi开发者_运维百科rect to action2 which is in controller2.
as specified below... But it doesn't seems to work Please help me!!
class controller1 {
def action1 = {
redirect(controller:'controller',action:'action2')
}
}
class controller2{
action2{
}
}
You need to give the correct controller name for controller attribute
redirect(controller:'controller2',action:'action2')
you need to do the redirect through your ajax call back function, e.g.: if you are using JQuery for example
$.ajax({
type: "POST",
url: "${createLink(controller:'controller1',action:'action1')}",
data: "dataToSend="somedata,
success: function(data){//data is the message rendered from action1
window.location = ${createLink(controller:'controller2',action:'action2')}
},
error: function(){
alert("Error from controller 1 action 1");
}
});
in your controller1 action1, you need to render back to the ajax event:
class controller1 {
def action1 = {
........
some code/logic
........
def message = "some message, success! or error!"
render(text:message, type:,contentType:'text/xml')
}
}
You can use render()
instead of redirect()
.
精彩评论