javascript - 如果不发送数据,Ajax 请求将无法工作

标签 javascript php jquery ajax authentication

我正在尝试向 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/

相关文章:

javascript - jquery 问题与 tr 单击更改复选框

javascript - Internet Explorer 10 - jQuery 单击选择选项不起作用

javascript - 使用 Jquery.animate 平滑 requestAnimationFrame?

javascript - 为什么 JQuery 不显示 div

javascript - jQuery 持久非事件绑定(bind)

php - CSS 路径问题 magento 2 安装

javascript - VM299 :1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON. 解析(<匿名>)

javascript - Uncaught Error : Container is not defined

javascript - 使用 jQuery 运行 PHP 并使用变量

javascript - 在 ajax 响应中返回 false 不起作用,并且表单不会取消提交