我正在尝试向PHP脚本发出AJAX请求以进行简单注销。
PHP仅执行以下操作:

<?php
session_start();
unset($_SESSION['autorizzato']);
$arr=array('result'=>"logout effettuato con successo");
$ris=json_encode($arr);
echo $ris;
?>


虽然AJAX请求看起来像这样:

$.ajax({
        type: 'POST',
        url: 'logout.php',
        async: false
       }).success(function(response){
       if (response['result']=="logout effettuato con successo")
            {
           change();
            }
            else alert("Errore nel logout");
        });
});


问题在于resonse ['result']看起来好像没有设置。
奇怪的是,如果我向AJAX请求添加数据字符串(如下所示:

$.ajax({
        type: 'POST',
        url: 'logout.php',
        async: false,
        dataType: 'json',
        data: sendstr
       }).success(function(response){
       if (response['result']=="logout effettuato con successo")
            {
           change();
            }
            else alert("Errore nel logout");
        });
});


其中sendstr是一个简单的JSON字符串化对象。
有人知道为什么吗?
先感谢您 :)

最佳答案

你的成功功能应该喜欢

 success(function(response){
    var returnsult=JSON.parse(response);
           if (returnsult.result=="logout effettuato con successo")
                {
               change();
                }
                else alert("Errore nel logout");
            });

关于javascript - 不发送数据就无法使用Ajax请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20174057/

10-11 08:04