Kv#. I have a form and call checklogin() function via jquery ajax call. When in controller, I check if valid email and password, if valid, I want to redirect to user profile action. It does call all that code but it is still sending the output back to my login page that then display the entire user profile page in the div I send the ajax call to (if invalid, it says: try again).
Anyway, I can't see why I can't redirect from within an action to another action that cancels the previous script. I've tried url:;redirect and request:: redirect. Such a simle request that works without the framework on ajax calls.
Is this possible on server side in the controller class?
The redirect call is being called like an ajax call and not redirecting to the page. It call it like ajax, gets the output, snd sends that back to my original ajax call. But can't redirect from ajax call. Any help is appreciated.
Javascript from XHR response is not executed by default. JQuery may eval() them though, but I'm not sure it's the best way to handle it. Additionally, 'location.href' sets location of the whole document, while OP wanted to display login box in a specific div, if I got that right. Anyhow, I'd first check if browser XHR and/or JQuery are supposed to follow redirects at all. If they are, then there's probably a problem with your code, if not, it may be better to look for another approach, such as requesting login route using Kohana HMVC and thus avoiding redirect.
Thanks for you help. Sounds like I need to hack this (with JS) which is what I suspected. I already could do that but just don't understand why I can't simply request.redirect like in any other php code. Is it a security thing about the framework that if an ajax calls comes in, then that's the only way to go back out (return)? Seems strange to me. Anyway, I guess I got my answer and that is that a redirect from an ajax call does not work in Kohana.
Hi Tsdon, the reason why that is impossible actually is very simple.
A request.redirect from within Kohana does not return any content (like javascript) but instead returns a header, which tells the browser to not open the location that is currently opened, but a new one. Now, with a normal page request, that means that not the requested page is loaded, but that the redirected page is loaded. i.e. call for page A -> at server redirect to page B -> server tells client (browser) via headers to load page B -> client calls for page B -> server returns page B. Thus, there is communication between server and client where the server tells the client to load another page.
Now, with AJAX the client (browser) might either not listen to this header (so that an empty page is returned and the div will become empty), or it will indeed call for page B, and put that in the div. Either way, the *original* page will never get redirected, as the server just tells the client to redirect the AJAX call, not the page that fired the AJAX call.
Yes, it does. Thanks for the explanation. So it's a redirect but different in the traditional way of what I've seen with straight php, .net, etc. Ok, at least I know what to do and thanks again.
I'm a .Net developer who is now using Kohana and php on 3 client projects so I'm enjoying it alot and plan to use more of.
That's great. It's by the way actually quite common to redirect via headers. You can find more information on this on the php.net page http://php.net/manual/en/function.header.php