[英]Redirect within Ajax call
I need to redirect an Ajax call without letting the client know.我需要在不让客户知道的情况下重定向 Ajax 调用。
getPage.php is an application which returns html etc. for a given query. getPage.php 是一个应用程序,它为给定的查询返回 html 等。
getPage.php?hash=interesting-article-title and getPage.php?page=3 returns the same. getPage.php?hash=interesting-article-title和getPage.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. 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.
I tried doing this and it returns nothing.我试过这样做,它什么也没返回。 The ajax call does not seem to want to be redirected.
ajax 调用似乎不想被重定向。
Does anyone have any idea what could solve this double-cache problem?有谁知道什么可以解决这个双缓存问题?
I would like to use 301 redirects because they are cacheable.我想使用 301 重定向,因为它们是可缓存的。 This way the browser would automatically call page=3 when it is told to fetch hash=insteresting-article-title .
这样,当告诉浏览器获取hash=insteresting-article-title时,浏览器会自动调用page=3 。
Code examples:代码示例:
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);
}
});
}
The reason I need the redirect to happen is because I get the id from a HASH when the user uses his back and forth browser buttons.我需要重定向发生的原因是因为当用户使用他的来回浏览器按钮时,我从 HASH 获取 id。
When the user simply navigates back and forth I simply add +1 to the current id.当用户简单地来回导航时,我只需将 +1 添加到当前 ID。
getPage.php returns something like this: 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"}
Thanks!谢谢!
If you look at this answer , the correct behaviour for browsers is to not cache urls with query params.如果您查看此答案,浏览器的正确行为是不使用查询参数缓存 url。
I dont know if 301 redirect even works when you are basically accessing the same url.当您基本上访问相同的 url 时,我不知道 301 重定向是否有效。
I suggest you go through your code and only use one of those links if they return the same thing.我建议您通过您的代码 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. 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.
I am assuming you are doing this for SEO benefits?我假设您这样做是为了获得 SEO 的好处?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.