繁体   English   中英

在 Ajax 调用内重定向

[英]Redirect within Ajax call

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

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

getPage.php?hash=interesting-article-titlegetPage.php?page=3返回相同。

To avoid the client from having to cache both of these I would like getPage.php?hash=interesting-article-title to REDIRECT to getPage.php?page=3 via PHP header 301 and location.

我试过这样做,它什么也没返回。 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"}

谢谢!

如果您查看此答案,浏览器的正确行为是不使用查询参数缓存 url。

当您基本上访问相同的 url 时,我不知道 301 重定向是否有效。

我建议您通过您的代码 go 并且仅在它们返回相同内容时使用其中一个链接。

If you are using apache, mod-rewrites and a rewrite map file are an excellent way to solve this server side assuming your hash is a predefined list of terms that map to the specified IDs.

我假设您这样做是为了获得 SEO 的好处?

暂无
暂无

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

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