how redirect in AJAX requests

8 years ago

mlha
Member | 58
+
0
-

NonAJAX solution:

  1. Nette will create form component and send it to user.
  2. User will complete and submit form.
  3. Nette will recreate form component to process it.
  4. Nette will call redirect.

…back to “start”…

1. Nette will create form component and send it to user. But form component will not be same (submited data changes it).

Now if i want to rebuild form component into independent AJAX component:

  1. Nette will create form component and send it to user.
  2. User will complete and submit(AJAX) form.
  3. Nette will create form component again to process it.
  4. Nette will call redirect(AJAX). But there is not any AJAX redirect!?!?

If i use redirect in 4. then whole page is reloaded :(
How to do AJAX request redirect?

8 years ago

Vojtěch Dobeš
Member | 1317
+
0
-

I am not sure what you mean by your post, but… AJAX requests usually don't lead to redirect, that's their meaning – they're asynchronous. And yes, the basic AJAX script from Addons redirects if the payload contains key redirect (Nette creates it in AJAX request when $this->redirect(... is called).

8 years ago

redhead
Member | 1315
+
0
-

If the redirect isn't needed (in most cases it isn't), the logic on the server side is usually done this way:

...
// some actions
...
if(!$this->isAjax()) {
    $this->redirect('this'); // or any other destination
}
// for '$this' being a presenter in this case

Last edited by redhead (2012-05-13 11:04)

8 years ago

mlha
Member | 58
+
0
-

vojtech.dobes wrote:

I am not sure what you mean by your post, but… AJAX requests usually don't lead to redirect, that's their meaning – they're asynchronous. And yes, the basic AJAX script from Addons redirects if the payload contains key redirect (Nette creates it in AJAX request when $this->redirect(... is called).

Heureka! That's solution. I have changed “AJAX Nette Framework plugin for jQuery”:

...
    if (payload.redirect) {
        //window.location.href = payload.redirect;
        $.post($.nette.href = payload.redirect, $.nette.success);
        return;
    }
...

Thanks

Last edited by mlha (2012-05-15 21:23)