PHP标头(“Location:index.html”)或PHP中的javascript不起作用
我正在制作一个注销按钮,通过AJAX调用调用PHP脚本,但不知何故,我的PHP没有重定向。而且,当我尝试使用javascript时,它也不起作用 我试过以下方法:PHP标头(“Location:index.html”)或PHP中的javascript不起作用,javascript,php,jquery,header,Javascript,Php,Jquery,Header,我正在制作一个注销按钮,通过AJAX调用调用PHP脚本,但不知何故,我的PHP没有重定向。而且,当我尝试使用javascript时,它也不起作用 我试过以下方法: 添加ob\u start()和ob\u end\u flush() 更改退出到退出() 更改标题(“位置:http://localhost/page_login.html) 到echo(“location.href=http://localhost/page_login.html“;”;”; 当我直接在我的URL中打开php脚本时
- 添加
和ob\u start()
ob\u end\u flush()
- 更改
退出代码>到
退出()代码>
- 更改
到标题(“位置:http://localhost/page_login.html)
echo(“location.href=http://localhost/page_login.html“;”;”;
当您直接调用php文件时,
头('位置:http://localhost/page_home.html“);
将位置字段添加到HTTP头,告诉浏览器将用户重定向到指定位置。
如果您通过ajax调用它,jQuery并不关心这个头字段。
您需要的是:
$('#logout_button').click(function(){
//Insert your ajax call here if you need to do something server side like destroy the session
window.location.replace('http://localhost/page_home.html');
})
编辑:正如评论中提到的,使用带有
标题('位置:http://localhost/page_home.html“);
在其中。当您直接调用php文件时,头('Location:http://localhost/page_home.html');
将位置字段添加到HTTP头,告诉浏览器将用户重定向到指定位置。
如果您通过ajax调用它,jQuery并不关心这个头字段。
您需要的是:
$('#logout_button').click(function(){
//Insert your ajax call here if you need to do something server side like destroy the session
window.location.replace('http://localhost/page_home.html');
})
编辑:正如评论中提到的,使用带有
标题('位置:http://localhost/page_home.html“);
在其中。在ajax内部的Javascript中使用location.href
重定向或重新加载,而不是在.php
文件中
$('#logout_button').click(function(){
$.ajax({
type: 'POST',
url: 'logout.php',
dataType: "json",
error: function(response) { console.log(JSON.stringify(response))},
success: function(data){
location.href = "/page_home.html";
//console.log(data);
}
});
});
还可以将响应数据从.php
发送到注销/session destroy
,如
<?php
// Code here to logout or session destroy
// return json or string
$response["success"] = true;
echo json_encode($response);
?>
在ajax内部的Javascript中使用
location.href
重定向或重新加载,而不是在.php
文件中
$('#logout_button').click(function(){
$.ajax({
type: 'POST',
url: 'logout.php',
dataType: "json",
error: function(response) { console.log(JSON.stringify(response))},
success: function(data){
location.href = "/page_home.html";
//console.log(data);
}
});
});
还可以将响应数据从.php
发送到注销/session destroy
,如
<?php
// Code here to logout or session destroy
// return json or string
$response["success"] = true;
echo json_encode($response);
?>
您对AJAX在这种情况下的工作方式有误解。当您执行AJAX调用时,会将一个单独的请求作为您的帖子发送到给定的URL。这不会固有地影响您在客户端窗口中的原始页面。本质上,发生的情况是:
logout.php
logout.php
运行其代码并返回对原始页面的响应(本例中为成功/失败;如果php页面中有return
,则返回一些数据)window.location.href
:
$('#logout_button').click(function(){
$.ajax({
type: 'POST',
url: 'logout.php',
error: function(response) { console.log(JSON.stringify(response))},
success: function(data){
window.location.href = "http://localhost/page_home.html";
console.log(data);
}
});
});
您对AJAX在这种情况下的工作方式有误解。当您执行AJAX调用时,会将一个单独的请求作为您的帖子发送到给定的URL。这不会从本质上影响您在客户端窗口中的原始页面。实际上,发生的情况是:
logout.php
logout.php
运行其代码并返回对原始页面的响应(本例中为成功/失败;如果php页面中有return
,则返回一些数据)window.location.href
:
$('#logout_button').click(function(){
$.ajax({
type: 'POST',
url: 'logout.php',
error: function(response) { console.log(JSON.stringify(response))},
success: function(data){
window.location.href = "http://localhost/page_home.html";
console.log(data);
}
});
});
哎哟,这只是我写这个示例代码时的一个输入错误。这不是问题。PHP脚本仍然没有重定向。当您进行AJAX调用时,您是否希望进行该调用的页面被重定向?因为这不是它的工作方式。只有AJAX调用本身被重定向,并且您得到的结果将是be它被重定向到的页面。是的,除了我在一个页面(比如主页)上之外,我单击了注销按钮。然后主页应该重定向到登录页面。根据我描述的注销情况,您建议我如何重定向?您设置了apache(或其他)吗要为带有html扩展的PHP脚本提供服务,或者您提到的页面都是带有
.PHP
扩展的PHP脚本吗?您使用什么来维护用户会话?我没有看到会话_uu start
或在注销.PHP
中删除会话的任何内容-注销页面没有发送响应,因此ajaxcallback永远不会做任何事情such@user3262578我不知道你为什么要在这里首先使用AJAX。只需在submit按钮周围放置一个表单标记,该表单标记具有表单标记属性method=“POST”
和action=“logout.php“
哎哟,我写这个示例代码只是打错了。这不是问题所在。PHP脚本仍然没有重定向,当您进行AJAX调用时,您是否希望