简体   繁体   中英

get json data back from ajax call

my question is: How can my php script send json type data and received back into the success or complete function?

I was trying to get this chatfunction to work on my website Because it diddn't work, I created a minimized portion off the code to check if it had something to do with the json method.

I only tested if I could get a sessionname back after the phpscript was proccessed What I get back is "undefined" instead of "johndoe".

I have no idea what could be the problem. Obviously, the script has worked fine for others, if you see the comments on the creators page.

this is my testingcode

<?php
session_start(); 
$_SESSION['username'] = "johndoe" ;// Must be already set
?>

<script type="text/javascript" src="includes/jquery.js"></script>
<script language="JavaScript">
$(document).ready(function(){
 $("#testjson").click(function(e){
 startJsonSession();

    return false;
    });


function startJsonSession(){  
    $.ajax({
        url: "jsontest.php?action=startjson",
        cache: false,
        dataType: "json",
        complete: function(data) {
            username = data.username;
            alert(username);
        }

    });
}


}); 
</script>

<?php
//the php script

if ($_GET['action'] == "startjson") { startjsonSession(); } 



function startjsonSession() {
    $items = '';


    /*if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }


    if ($items != '') {
        $items = substr($items, 0, -1);
    }*/

header('Content-type: application/json');
?>
{
        "username": "<?php echo $_SESSION['username'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php


    exit(0);
}

?>

thanks, Richard

Richard, you should look into the json_encode() function in PHP. It will convert your array to JSON quickly, and keep you from having to deal with the smaller nuances of JSON syntax with large amounts of data.


Update: Modified Code

<?php

    session_start(); 
    $_SESSION['username'] = "johndoe" ;// Must be already set

?>

<script type="text/javascript" src="includes/jquery.js"></script>
<script language="JavaScript">
$(document).ready(function(){

    $("#testjson").click(function(e){
        startJsonSession();
        return false;
    });

    function startJsonSession(){  
        $.ajax({
            url: "jsontest.php?action=startjson",
            cache: false,
            dataType: "json",
            complete: function(data) {
                username = data.username;
                alert(username);
            }

        });
    }

}); 
</script>

<?php

    if ($_GET['action'] == "startjson") { 
        startjsonSession(); 
    } 

    function startjsonSession() {
        $items = '';

        print json_encode(array(
            "username" => "bob",
            "items" => array(
                "item1" => "sandwich",
                "item2" => "applejuice"
            )
        ));
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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