4

您好我有一个登录系统的 jQuery ajax 请求。起初,它工作得非常好。但经过几次尝试,它才开始出现负面反应。我检查了萤火虫,它说在我的情况下响应是“已连接”。但是 ajax 响应只显示“Not_connected”。我不知道该怎么办:(。请帮助我。

这是我的jQuery代码:

var data_str = "username="+usrn+"&password="+pwd;
$.ajax({
    type: "POST",
    url: "index.php?rnd=" + Math.random(),
    data : data_str,
    complete : function(xhr,data){
        if(data == 'connected'){window.location.href = 'admin.php';}
            else if(data = 'not_connected'){ error_gen.html('Invalid username or password'); }
            alert(data);
        }
});

至于 PHP 代码:

$log_result = $u_obj->login_user();
if($log_result == true)/*user is connected*/
{
    echo 'connected';
    exit;/*stoping the script after sending the result*/
}
    elseif($log_result == false)/*error while logging in*/
    {
        echo 'not_connected';
        exit;/*stoping the script after sending the result*/
    }
4

3 回答 3

5

看看这个线程:Is it possible to cache POST methods in HTTP?

可能有一些标头现在使浏览器缓存响应(尽管它是 POST)。

您还可以添加 write 而不是 rnd=" + Math.random()

$.ajax({
    type: "POST",
    cache: false,
    ..
于 2012-03-26T04:20:33.230 回答
2

会不会是浏览器缓存?尝试添加这个 $.ajaxSetup({ cache: false });

于 2012-03-26T04:17:17.390 回答
0

您使用了错误的 $.ajax 选项来检索结果。您应该使用成功选项。只需更改

complete : function(xhr,data){

线为

success : function(data){

它应该工作。

于 2012-03-26T05:12:50.360 回答