javascript - Ajax request won't work without sending data - Stack Overflow

I'm trying to make an AJAX request to a PHP script for a simple logout.The PHP just does the foll

I'm trying to make an AJAX request to a PHP script for a simple logout. The PHP just does the following:

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

While the AJAX request looks something like this:

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

Problem is that resonse['result'] looks like it's unset. The curious thing is that if I add to the AJAX request a data string (like this:

$.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");
        });
});

where sendstr is a simple JSON stringified object. Anyone knows why? Thank you in advance :)

I'm trying to make an AJAX request to a PHP script for a simple logout. The PHP just does the following:

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

While the AJAX request looks something like this:

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

Problem is that resonse['result'] looks like it's unset. The curious thing is that if I add to the AJAX request a data string (like this:

$.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");
        });
});

where sendstr is a simple JSON stringified object. Anyone knows why? Thank you in advance :)

Share Improve this question asked Nov 24, 2013 at 11:42 Stefano KiraStefano Kira 1752 gold badges3 silver badges16 bronze badges 2
  • You have dataType:'json' that's the way you tell jQuery you are expecting JSON from the server. – Charlie Affumigato Commented Nov 24, 2013 at 11:50
  • Check your server response in network tab, in console check if it can be parsed by JSON.parse() – Mohebifar Commented Nov 24, 2013 at 11:53
Add a ment  | 

2 Answers 2

Reset to default 1

your success function should do like

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

Either you go this way:

$.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");
    });
});

Or

  $.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");
    });
   });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745337117a4623157.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信