最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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");
    });
   });
发布评论

评论列表(0)

  1. 暂无评论