php中的AJAX问题
我正在尝试编写非常简单的AJAX,但有一些问题。 以下是我的ajax代码:php中的AJAX问题,php,ajax,Php,Ajax,我正在尝试编写非常简单的AJAX,但有一些问题。 以下是我的ajax代码: <div id="userHeader"> <img src="/images/logout.png" alt="logout" title="exit" onclick="ajaxLogOut()" style="cursor: pointer;"> </div> <script type="text/javascript"> function ajaxLogOut(
<div id="userHeader">
<img src="/images/logout.png" alt="logout" title="exit" onclick="ajaxLogOut()" style="cursor: pointer;">
</div>
<script type="text/javascript">
function ajaxLogOut(){
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/user/logout.php",true);
xmlhttp.send();
// location.reload();
}
函数ajaxLogOut(){
var-xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
}
其他的
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“/user/logout.php”,true);
xmlhttp.send();
//location.reload();
}
以及我的logout.php:
<?php
session_start();
if (isset($_SESSION['log_in'])) {
unset($_SESSION['log_in']);
unset($_SESSION['username']);
session_destroy();
}
?>
问题是,当我注释location.reload()并手动重新加载页面时,它会正常工作。但如果取消对该行的注释,则会刷新页面,但我仍在登录。尝试:
xmlhttp.open("GET","/user/logout.php",false);//sync mode
xmlhttp.open("GET","/user/logout.php",true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
location.reload();
}
}
xmlhttp.send(null);
或(带callbak的异步模式)-建议:
xmlhttp.open("GET","/user/logout.php",false);//sync mode
xmlhttp.open("GET","/user/logout.php",true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
location.reload();
}
}
xmlhttp.send(null);
还可以尝试异步模式。祝你好运(别忘了接受我的答案,把它标记为
正确的:)谢谢)回答得太快了,不得不等到堆栈溢出让我接受它。:)