简体   繁体   English

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

[英]Ajax request won't work without sending data

I'm trying to make an AJAX request to a PHP script for a simple logout. 我正在尝试向PHP脚本发出一个AJAX请求以进行简单的注销。 The PHP just does the following: PHP只执行以下操作:

<?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请求看起来像这样:

$.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请求添加一个数据字符串(如下所示:

$.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. 其中sendingtr是一个简单的JSON字符串化对象。 Anyone knows why? 谁知道为什么? Thank you in advance :) 先感谢您 :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM