开发者

Symfony: How to redirect to a new page in an AJAX call?

I've got jquery sending an AJAX request that executes some checks and then should EITHER redirect to another page or return data. Everything works fine except I'm unable to force the redirect in my actions file. I've tried:

$this->redirect('@new_page'); // DOESN'T WORK

And...

$this->context->getController()->redirect('@new_page'); // DOESN'T WORK

I think there's a way to return the redirect URL back to Jquery and redirect from there but I'd prefer to handle the redirect in my action.

Is this possible?

Thanks

UPDATE:

The final working solution the above was to retu开发者_Python百科rn TRUE/FALSE back to the Ajax request and add the following to my Jquery function:

if(data == true) window.location.href = "<?php echo url_for('@new_page') ?>";
else // do other stuff

The new page URL didn't need to be dynamic and was able to be inserted on first page load. In this case, TRUE/FALSE sufficed for the data to be returned for the else clause.


Remember, with an AJAX action the result is returned (generally) to a callback handler or a DIV element in the page. Thus, the redirect you are trying to use only refers to this AJAX action's response, not the entire HTTP page request. The redirect is probably working fine from within symfony, just that the response it returns is being fed to your DIV or callback handler, and it doesn't know what to do with the HTML. You will indeed need to fire the redirect or refresh from within the AJAX callback handler.

You may want to consider eschewing a full redirect in favour of using the error callback and displaying another DIV with error message content, but this error callback could be used for the redirect too, if you prefer.


I have handle this one by below code,while remaining in controller

// some other code goes here
// run this in special condition like if
$url = $this->generateUrl('fos_user_security_login');
echo '<script>window.location = "'.$url.'";</script>';die;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜