javascript - Grails: Page won't redirect after completed Ajax call - Stack Overflow

This is my ajax call: <body onload ="${remoteFunction(controller:'accountManagement'

This is my ajax call: <body onload = "${remoteFunction(controller:'accountManagement', action:'createAccount', params:[facebookUID: params.facebookUID, gender: params.gender, firstName: params.firstName, lastName: params.lastName])}">

In my controller I have a redirect instruction at the end:

def createAccount = {

    if(user.save(flush:true) == null){
        ... 
    }
    else{
        ....

        for(int i = 0; i < categories.length; i++){
            ...
        }

        println "save to database: successful"

    }

    // Redirect to index action of person controller.
    redirect(controller:'user', action: 'authenticate');


}
`

It does not redirect. Instead, it stays on the same loading page that it started on.

This is my ajax call: <body onload = "${remoteFunction(controller:'accountManagement', action:'createAccount', params:[facebookUID: params.facebookUID, gender: params.gender, firstName: params.firstName, lastName: params.lastName])}">

In my controller I have a redirect instruction at the end:

def createAccount = {

    if(user.save(flush:true) == null){
        ... 
    }
    else{
        ....

        for(int i = 0; i < categories.length; i++){
            ...
        }

        println "save to database: successful"

    }

    // Redirect to index action of person controller.
    redirect(controller:'user', action: 'authenticate');


}
`

It does not redirect. Instead, it stays on the same loading page that it started on.

Share Improve this question edited Sep 11, 2012 at 20:32 tim_yates 171k29 gold badges358 silver badges353 bronze badges asked Sep 11, 2012 at 20:08 Orca NinjaOrca Ninja 8331 gold badge15 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Browsers won't redirect if an AJAX call returns a redirect. If you want to send the user to a new page after an AJAX call, you'll need to do so yourself in Javascript. Example:

def url = createLink(controller: 'user', action: 'authenticate')
render(contentType: 'text/html', text: "<script>window.location.href='$url'</script>")

Ensure the AJAX response gets rendered by the browser. With the grails remoteFunction tag you should specify an element to update with the update attribute.

Redirecting the ajax call only redirects that call, not the whole page. To get the whole page to redirect, you either need to capture the redirected ajax call in the JavaScript and do a redirect from the JavaScript, or you need to have the page-level action do the redirect.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385197a4625408.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信