在Ajax调用中重定向

时间:2009-03-25 00:20:18

标签: php ajax caching header

我需要在不让客户知道的情况下重定向Ajax调用。

getPage.php是一个为给定查询返回html等的应用程序。

getPage.php?hash = interesting-article-title getPage.php?page = 3 会返回相同内容。

为了避免客户端必须缓存这两个,我希望 getPage.php?hash = interesting-article-title 重定向到 getPage.php?page = 3 通过PHP标题301和位置。

我尝试过这样做,它什么也没有返回。 ajax调用似乎不想重定向。

有谁知道什么可以解决这个双缓存问题?

我想使用301重定向,因为它们是可缓存的。这样,当被告知要获取 hash = insteresting-article-title 时,浏览器会自动调用 page = 3

代码示例:

    function handleInit(event) {
        if (event.path != "/") {
            var hash = event.path.replace(/\//,"").replace(/\?/g,"");
            getPage(hash);
            currentState.hash = hash;
        } else {
            currentState.id = 1;
        }
        SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
    }


    function chapter_forward() {
        if (!freeze) {
            freeze = true;
            getPage(currentState.id - 1);
        }
    }


    function ajax_data(id) {
        return typeof(id) == "string" ? "hash=" : "page=";
    }

    function getPage(id) {
        $.ajax({
            method: "get",
            url: getPage.php,
            data: ajax_data(id) + id.toString(),
            dataType: 'json',
            timeout: 2000,
            error: function() {
                $("#space-3").html('40104 - Request Timeout');
            },
            complete: function() {},
            success: function(result) {
                handleContent(result);
            }
        });
    }

我需要重定向的原因是因为当用户使用他的来回浏览器按钮时,我从HASH获取id。

当用户只是来回导航时,我只需将+1添加到当前ID。

getPage.php会返回如下内容:

{"article_id":"3","hash":"music-in-your-ear","title":"Music in your ear.","html":"Blah blah article 3","modified":"Fri, 20 Mar 2009 02:46:00 GMT"}

谢谢!

2 个答案:

答案 0 :(得分:1)

如果查看this answer,浏览器的正确行为是不使用查询参数缓存网址。

当你基本上访问同一个网址时,我不知道301重定向是否有效。

我建议您浏览一下代码,如果他们返回同样的内容,则只使用其中一个链接。

答案 1 :(得分:1)

如果您使用的是apache,则mod-rewrites和重写映射文件是解决此服务器端的绝佳方法,假设您的哈希值是映射到指定ID的预定义术语列表。

我假设你这样做是为了SEO的好处吗?