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