I am working on an AJAX login/logout form. The login works fine, but when someone clicks on "logout" I am having a few problems.
Javascript:
$(开发者_JAVA技巧"#logout").live("click", function(){
$.post("/hd/ajax.php",{ logout: "1" }, function(data) {
alert(data);
});
});
Ajax.php:
if(isset($HTTP_POST_VARS['logout']) && $HTTP_POST_VARS['logout'] == 1)
{
$this->auth = false;
$this->un = false;
$this->id = false;
setcookie ($this->cun, "", 1, "/", $this->chn, 0);
setcookie ($this->cpw, "", 1, "/", $this->chn, 0);
echo "1";
return 1;
}
When clicked, the request is sent and user is logged out just fine. The Alert(data) does not return anything though, even though the PHP function is returning the "1"
I have posted to the ajax.php manually, and I do get the text "1" coming to my browser.
I just noticed aswell, that if I remove the alert() from the callback, the user is not actually logged out via the called PHP script. I have to alert(data) in order for the user to be logged out.
The only thing I can think is that I am sending Header information(clearing cookies) in the PHP file before data is returned. Is this OK? Or is this the cause of my problems?
I feel like I am missing something obvious, please let me know if so.
The only thing I would say is maybe try doing:
return "1"
The ajax request uses intelligence to try and figure out the return datatype and there might be an issue since it isn't a string and not json or html.
精彩评论