views:

648

answers:

1

Hi,

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 return 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.

+2  A: 

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.

Raise
Makes perfect sense thanks. Will look into the Jquery/callback redirect.
Tom