简体   繁体   English

jQuery AJAX重定向问题

[英]jQuery AJAX Redirection problem

please consider this: On page AI have a link that takes you to page B when JS is off, but when JS is on, I want to replace content on current page with content from the page B. 请考虑以下几点:在AI页上,当JS关闭时,您可以转到B页,但是当JS打开时,我想用B页中的内容替换当前页面上的内容。

Pages A and B are in fact the same script that is able to tell AJAX calls from regular ones and serve the content appropriately. 实际上,页面A和B是相同的脚本,能够告诉普通AJAX调用并适当地提供内容。 Everything works fine, as long as there are no redirects involved. 只要不涉及重定向,一切都可以正常工作。

But, sometimes there is a 301 redirect and what seems to be happening is that client browser then makes a second request, which will return with a 200 OK. 但是,有时会有一个301重定向,并且似乎正在发生的事情是客户端浏览器随后发出了第二个请求,该请求将返回200 OK。 Only the second request is sent without a X-Requested-With header, therefore I cannot tell within my script wether it came from AJAX or not, and will send a complete page instead of just the content. 仅发送第二个请求而没有X-Requested-With标头,因此我无法在脚本中告诉它是否来自AJAX,并且将发送完整的页面而不是仅发送内容。

I have tried checking for 301 status code in my error, success, and complete handlers but none of them worked. 我尝试检查我的错误,成功和完整的处理程序中的301状态代码,但它们均无效。 It seems to be handling the 301 behind the scenes. 它似乎正在处理301背后的问题。

Could anyone help me with this? 有人可以帮我吗?

jQuery 1.4, PHP 5 jQuery 1.4,PHP 5

Edit: People requested the code to this, which I didn't think was necessary but here goes: 编辑:人们要求对此进行编码,我认为这不是必需的,但是这里是这样:

// hook up menu ajax loading
$('#menu a').live("click", function(){
    // update menu highlight
    if($(this).parents('#menu').size() > 0){
        $("#menu>li").removeClass("current_page_item");
        $(this).parent().addClass("current_page_item");
    }
    // get the URL where we will be retrieving content from 
    var url = $(this).attr('href');

    window.location.hash = hash = url;

    $.ajax({
        type: "GET",
        url: url,
        success: function(data){
            // search for an ID that is only present if page is requested directly 
            if($(data).find('#maincontent').size() > 0){
                data = $(data).find('#maincontent .content-slide *').get();
            }
            // the rest is just animating the content into view
            $("#scroller").html(data);
            $('.content-slide').each(setHeight);

            $('.content-slide').animate({
                    left: "0px"
                }, 1000, 'easeOutQuart', 
                function(){
                    $('#home').css("left", "-760px").html(data);
                    $('#scroller').css("left", "-760px");
                    $('.content-slide').each(setHeight);
                }
            );
        }
    });

    return false;
});

Due to the fact that the headers aren't being passed properly after the redirect, it seems like a good option would be to allow for a query string variable to indicate the isAjax status of a request as well as the x-requested-with header. 由于标头在重定向后未正确传递,因此似乎不错的选择是允许查询字符串变量指示请求的isAjax状态以及x-requested-with标头。 So if your 301 redirects are being generated by an .htaccess file, you'd just need to pass the query string arguments onto the new path when redirecting. 因此,如果您的301重定向是由.htaccess文件生成的,则只需在重定向时将查询字符串参数传递到新路径即可。 For instance: 例如:

RewriteEngine on
RewriteRule pageB.php$ pageC.php?%{QUERY_STRING} [L,R=301]

So your request for pageB.php?isAjax=1 will be pageC.php?isAjax=1, resulting in the properly formatted response from the server. 因此,您对pageB.php?isAjax = 1的请求将是pageC.php?isAjax = 1,从而导致服务器响应格式正确。

Since you load pageB with either direct access and using AJAX, I usually put the simple if in page like that. 由于您可以直接访问并使用AJAX加载pageB,因此我通常将简单的if放在这样的页面中。 Page B will looks like this: B页将如下所示:

<?php $is_ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); ?>
<?php if ( ! $is_ajax ): ?>
<html>
<!-- full page content for direct access -->
<?php endif; ?>
<!-- page content that for AJAX request and direct access -->
<div id="maincontent">
<!-- content here -->
</div>
<!-- end of page content that for AJAX request and direct access -->
<?php if ( ! $is_ajax ): ?>
<!-- rest of page content -->
</html>
<?php endif; ?>

Then your jQuery code will not need to do checking, just get the page and add it to scroller . 然后,您的jQuery代码将无需进行检查,只需获取页面并将其添加到scroller

//I prefer just user $.get
$.get(url, {}, 
  function(data){
    // the rest is just animating the content into view
    $("#scroller").html(data);
    //...
  });

With this, I don't need to filter the response data from AJAX request. 这样,我就不需要从AJAX请求中过滤响应数据。

I have one question for you. 我有一个问题要问你。 What this code do? 这段代码做什么?

window.location.hash = hash = url;

Maybe that what cause the weird redirect. 也许是什么原因导致了奇怪的重定向。

If you still experience the weird redirect, debugging using Firebug and see the apache log might help you find the bug source. 如果您仍然遇到奇怪的重定向,请使用Firebug调试并查看apache日志可能会帮助您找到错误的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM