我正在尝试向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");
});
});
问题是,共鸣['结果']看起来像是未设置的。 奇怪的是,如果我向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");
});
});
其中senttr是一个简单的JSON字符串化对象。 谁知道为什么? 提前谢谢你:)
答案 0 :(得分:1)
你的成功功能应该像
一样 success(function(response){
var returnsult=JSON.parse(response);
if (returnsult.result=="logout effettuato con successo")
{
change();
}
else alert("Errore nel logout");
});
答案 1 :(得分:1)
你要么这样:
$.ajax({
type: 'POST',
url: 'logout.php',
async: false
}).success(function(response){
response=JSON.parse(response);//convert JSON string to JS object
if (response['result']=="logout effettuato con successo")
{
change();
}
else alert("Errore nel logout");
});
});
或者
$.ajax({
type: 'POST',
url: 'logout.php',
async: false,
dataType: 'json' /* Tell jQuery you are expecting JSON */
}).success(function(response){
if (response['result']=="logout effettuato con successo")
{
change();
}
else alert("Errore nel logout");
});
});