I am using Ajax in my project so in header.php i am using Javascript code for sending the XMLHttpRequest to the server. I am including this header.php file in every page of the website. Problem is here that in signin.php page i am using header function. That is not working due to the Ajax code i am using in the header. When i remove that code from the header.php. header function working fine.Please help me out.
我在我的项目中使用Ajax,所以在header.php中我使用Javascript代码将XMLHttpRequest发送到服务器。我在网站的每个页面中都包含了这个header.php文件。问题是在signin.php页面我正在使用头功能。由于我在标头中使用的Ajax代码,这是行不通的。当我从header.php中删除该代码时。标题功能工作正常。请帮帮我。
header function i am using in signin.php: header("location:index.php");
我正在使用signin.php:header(“location:index.php”);
Ajax Code using in header.php :
在header.php中使用的Ajax代码:
<script>
function favourite(str)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("like-div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","add_to_favourites.php?"+str,true);
xmlhttp.send();
}
</script>
1 个解决方案
#1
0
Actually, the Location
header does work, but it redirects the Ajax request that runs in the background instead of triggering a full URL change in the browser. Browsers handle this redirection of the Ajax request transparently, so what ends up happening is that the contents of #like-div
are replaced by the full HTML code of index.php.
实际上,Location标头确实有效,但它重定向在后台运行的Ajax请求,而不是在浏览器中触发完整的URL更改。浏览器透明地处理Ajax请求的这种重定向,因此最终发生的事情是#like-div的内容被index.php的完整HTML代码替换。
Obviously this is not the intended behavior in this case, so what we need to do is detect somehow that the redirect has happened during an Ajax request, and send back some JavaScript to manually set document.location
in the browser.
显然这不是这种情况下的预期行为,所以我们需要做的是在Ajax请求期间以某种方式检测重定向,并发回一些JavaScript以在浏览器中手动设置document.location。
A possible solution is using jQuery for placing the Ajax request (as it supports browser-independent execution of scripts in the response), and sending the JavaScript redirect code instead of a Location
header when an XMLHttpRequest is detected:
一种可能的解决方案是使用jQuery放置Ajax请求(因为它支持响应中的脚本独立于浏览器),并在检测到XMLHttpRequest时发送JavaScript重定向代码而不是Location头:
JavaScript
$("#like-div").load("add_to_favourites.php?" + str);
/* see how much easier it is with jQuery? */
header.php
/* Function for Ajax-friendly redirects */
function redirect_with_ajax($url, $permanent = false)
{
@ob_end_clean();
if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{
print('<script> location.href = ' . json_encode($url) . '; </script><div>You are being redirected <a href="' . htmlentities($url, ENT_QUOTES, "UTF-8") . '">here</a>.</div>');
}
else
{
header($_SERVER["SERVER_PROTOCOL"] . " " .
($permanent ? "301 Moved Permanently" : "302 Found"));
header("Location: " . $url);
}
exit;
}
/* Do the redirect */
redirect_with_ajax("index.php");
The code is untested and may contain errors. Use it with caution.
代码未经测试,可能包含错误。请谨慎使用。
#1
0
Actually, the Location
header does work, but it redirects the Ajax request that runs in the background instead of triggering a full URL change in the browser. Browsers handle this redirection of the Ajax request transparently, so what ends up happening is that the contents of #like-div
are replaced by the full HTML code of index.php.
实际上,Location标头确实有效,但它重定向在后台运行的Ajax请求,而不是在浏览器中触发完整的URL更改。浏览器透明地处理Ajax请求的这种重定向,因此最终发生的事情是#like-div的内容被index.php的完整HTML代码替换。
Obviously this is not the intended behavior in this case, so what we need to do is detect somehow that the redirect has happened during an Ajax request, and send back some JavaScript to manually set document.location
in the browser.
显然这不是这种情况下的预期行为,所以我们需要做的是在Ajax请求期间以某种方式检测重定向,并发回一些JavaScript以在浏览器中手动设置document.location。
A possible solution is using jQuery for placing the Ajax request (as it supports browser-independent execution of scripts in the response), and sending the JavaScript redirect code instead of a Location
header when an XMLHttpRequest is detected:
一种可能的解决方案是使用jQuery放置Ajax请求(因为它支持响应中的脚本独立于浏览器),并在检测到XMLHttpRequest时发送JavaScript重定向代码而不是Location头:
JavaScript
$("#like-div").load("add_to_favourites.php?" + str);
/* see how much easier it is with jQuery? */
header.php
/* Function for Ajax-friendly redirects */
function redirect_with_ajax($url, $permanent = false)
{
@ob_end_clean();
if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{
print('<script> location.href = ' . json_encode($url) . '; </script><div>You are being redirected <a href="' . htmlentities($url, ENT_QUOTES, "UTF-8") . '">here</a>.</div>');
}
else
{
header($_SERVER["SERVER_PROTOCOL"] . " " .
($permanent ? "301 Moved Permanently" : "302 Found"));
header("Location: " . $url);
}
exit;
}
/* Do the redirect */
redirect_with_ajax("index.php");
The code is untested and may contain errors. Use it with caution.
代码未经测试,可能包含错误。请谨慎使用。