Answer the question
In order to leave comments, you need to log in
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]));
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)
}
$.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
if redirect via js, then just $.pjax(url, ...)
if via Yii, then you can't
— 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.
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.
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)
}
}
// 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)
}
if(!Yii::$app->request->isPjax || !Yii::$app->request->isAjax)
return $this->redirect(['group/show','id' => $id ]);
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 questionAsk a Question
731 491 924 answers to any question