A
A
Alexander N++2015-02-04 14:57:14
Yii
Alexander N++, 2015-02-04 14:57:14

Yii2 pajax and redirect, how to make redirect work without reloading the user's page?

Yii2 pajax and redirect, how to make redirect work without reloading the user's page?
Everything turned out to be much easier. HURRAY SOLUTION!Yo!

Yii::$app->response->getHeaders()->set('X-PJAX-Url',Url::to(['wall/show','id' => 2]));

And we immediately give the data of this page of this wall / show controller PS in Laravel, this trouble is approximately also solved.
What I visited before
I tried to override the
method in the impudent library code
options.error =  function (xhr, textStatus, errorThrown) {
            var container = extractContainer("", xhr, options)
            // Check redirect status code
            var redirect = (xhr.status >= 301 && xhr.status <= 303)
            // Do not fire pjax::error in case of redirect
            if(redirect) {
                   var conteiner1 =   xhr.getResponseHeader('X-PJAX-Container') || false; // этот заголовк я сам отправил
                    if(conteiner1){
                              pjaxReload($(conteinercontainer.url);
                              return false;
                    }
            }


            var allowed = redirect || fire('pjax:error', [xhr, textStatus, errorThrown, options])
            if (redirect || options.type == 'GET' && textStatus !== 'abort' && allowed) {
                locationReplace(container.url)
            }

It all works, but I don’t want to get into someone else’s room ....
Another way is global
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
        var originalPromise = jqXHR.promise(),
        newDefer = $.Deferred(function( defer ) {
            defer.promise( jqXHR );
            jqXHR.success = originalPromise.done;
            jqXHR.error = defer.fail;
        });
        originalPromise.fail(function(xhr, textStatus, errorThrown) {
            var isPajax = xhr.getResponseHeader('X-PJAX') || false;
            var redirect = (xhr.status >= 301 && xhr.status <= 303);
            if(redirect) {
                var conteiner =   xhr.getResponseHeader('X-PJAX-Container') || false;
                var isPajax = xhr.getResponseHeader('X-PJAX') || false;
                if(isPajax){
                    $.pajax.reload(conteiner , url);
                    return false;
                 }
                
            }
        });
    });

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Anton, 2015-02-04
@sanchezzzhak

if redirect via js, then just $.pjax(url, ...)
if via Yii, then you can't

F
FanatPHP, 2015-02-04
@FanatPHP

— So, — says Morkoveva. We need to draw seven red lines. All of them must be strictly perpendicular, and in addition, some need to be drawn in green, and some more - transparent. Do you think it's real?
“No,” says Petrov.

X
xtalen, 2015-02-04
@xom9lk

Explain what kind of redirect, where the redirect is called, on what event.
The redirect itself, as such, cannot happen without reloading the page, it has such a nature.

A
Alexander N++, 2015-02-11
@sanchezzzhak

jquery pajax if getting 301 to 301 response

options.error = function(xhr, textStatus, errorThrown) {
    var container = extractContainer("", xhr, options)
    // Check redirect status code
  var redirect = (xhr.status >= 301 && xhr.status <= 303)
  // Do not fire pjax::error in case of redirect
    var allowed = redirect || fire('pjax:error', [xhr, textStatus, errorThrown, options])
    if (redirect || options.type == 'GET' && textStatus !== 'abort' && allowed) {
      locationReplace(container.url)
    }
  }

then the function locationReplace is called
// Internal: Hard replace current state with url.
//
// Work for around WebKit
//   https://bugs.webkit.org/show_bug.cgi?id=93506
//
// Returns nothing.
function locationReplace(url) {
  if(!pjax.options.history) return;
  window.history.replaceState(null, "", pjax.state.url)
  window.location.replace(url)
}

As a result, you have to refuse redirect, and where pajax ajax does not work, use redirect
on older phones I use redirect (they were out of luck)
if(!Yii::$app->request->isPjax || !Yii::$app->request->isAjax)
              return $this->redirect(['group/show','id' => $id  ]);

N
Nurbek Nurjanov, 2018-09-15
@nurbek_nurjanov

Not quite clear.
It doesn't work for me with redirect.
There is a PostController/actionView(id)
controller In the page of this controller there are comments, with the ability to delete comments
All comments are wrapped in pjax
There is a CommentController/actionDelete(id)
controller There is a reverse redirect to the post page. that is back here PostController/actionView(id)
What should be put where. I tried to play with x-pjax-url :(((

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question