我的网站使用jQuery load
方法通过AJAX加载所有页面。我尽力使this tutorial适应Wordpress。
我今天的问题是当load
方法返回错误时(例如因链接断开而导致404),AJAX转换未完成,而页面根本不会改变。
- 如何在加载方法失败时执行某些操作?
编辑:我在文档中找到了解决方案。
section.load(url + ' .cd-main-content > *', function (response, status, xhr) {
//the code
}
现在它并没有像我想象的那样帮助我。因为response
按预期返回404页面内容,status
会返回错误并xhr
[object OBJECT]。我没有得到的是为什么response
html没有加载到div ...
- 我怎么知道这是404错误?
使用以下方法成功检查了404错误:
if (status == "error" && xhr.status == "404") {
console.log('this is a 404 error');
}
如果load
为xhr.status
或404
为status
,我尝试在success
函数条件中执行我的代码,但没有运气。还是行不通。任何人都可以帮助吗?
- 如何使用jQuery重定向到wordpress的404.php页面?
- 我该如何处理其他错误代码?
很多问题很抱歉......我不知道从哪里开始
以下是Javascript:
jQuery(document).ready(function (event) {
// select website's root url
var rootUrl = aws_data.rootUrl;
var isAnimating = false,
newLocation = '',
firstLoad = false;
// Internal Helper
$.expr[':'].internal = function(obj, index, meta, stack){
// Prepare
var
$this = $(obj),
urlinternal = $this.attr('href')||'',
isInternalLink;
// Check link
isInternalLink = urlinternal.substring(0,rootUrl.length) === rootUrl || urlinternal.indexOf(':') === -1;
// Ignore or Keep
return isInternalLink;
};
//trigger smooth transition from the actual page to the new one, excluding event on non relevant links
$('main').on('click', 'a[href]:internal:not(.no-ajaxy,.love-button,[href^="#"],[href*="#respond"],[href*="wp-login"],[href*="wp-admin"])', function (event) {
event.preventDefault();
//detect which page has been selected
var newPage = $(this).attr('href');
//if the page is not already being animated - trigger animation
if (!isAnimating) changePage(newPage, true);
firstLoad = true;
});
//detect the 'popstate' event - e.g. user clicking the back button
$(window).on('popstate', function (e) {
if (firstLoad) {
/*
Safari emits a popstate event on page load - check if firstLoad is true before animating
if it's false - the page has just been loaded
*/
var newPageArray = location.pathname.split('/'), //this is the url of the page to be loaded
//newPage = newPageArray[newPageArray.length - 1];
newPage = window.location.href;
if (!isAnimating && newLocation != newPage) changePage(newPage, false);
}
firstLoad = true;
});
function changePage(url, bool) {
isAnimating = true;
// trigger page animation
$('body').addClass('page-is-changing');
$('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
loadNewContent(url, bool);
newLocation = url;
$('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
});
//if browser doesn't support CSS transitions
if (!transitionsSupported()) {
loadNewContent(url, bool);
newLocation = url;
}
}
function loadNewContent(url, bool) {
// I don't understand the line below
url = ('' === url) ? rootUrl : url;
var newSection = 'cd-' + url.replace(rootUrl, "");
var section = $('<div class="cd-main-content ' + newSection + '"></div>');
// this ajax request helps me applied Wordpress classes on the body element, which is not being reloaded below
$.ajax({url: url,
success: function(data){
data = data.replace("<body", "<container").replace("body>", "container>");
var classes = $(data).filter("container").attr("class");
$("body").attr("class", classes + " page-is-changing");
}
});
section.load(url + ' .cd-main-content > *', function (event) {
// load new content and replace <main> content with the new one
$('main').html(section);
var delay = 1200;
//functions to execute after DOM is loaded
$(document).foundation();
makeFooterSticky();
window.scrollTo(0, 0);
$(".ajax-load-more-wrap").ajaxloadmore();
//ga('send', 'pageview', window.location.pathname);
setTimeout(function () {
//wait for the end of the transition on the loading bar before revealing the new content
$('body').removeClass('page-is-changing');
$('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
isAnimating = false;
$('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
});
if (!transitionsSupported()) isAnimating = false;
}, delay);
if (url != window.location && bool) {
//add the new page to the window.history
//if the new page was triggered by a 'popstate' event, don't add it
window.history.pushState({
path: url
}, '', url);
}
});
}
function transitionsSupported() {
return $('html').hasClass('csstransitions');
}
});
答案 0 :(得分:1)
我终于解决了我的问题。
在意识到响应返回了我想要的内容后,我猜了一下,并认为问题是返回的状态是404,实际上,200是我想要的。
所以我在404.php文件的开头添加了status_header(200);
,现在它完美无缺。
我希望放弃404状态代码不成问题,但这是我现在唯一的解决方案。