从ajax调用中获取json数据

时间:2021-12-02 19:44:05

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

我的问题是:我的php脚本如何发送json类型数据并收回成功或完整功能?

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.

我试图让这个聊天功能在我的网站上工作因为它不起作用,我创建了一个最小化的部分代码,以检查它是否与json方法有关。

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

我只测试了在phpscript被执行后我是否可以获得一个会话名称我得到的是“未定义”而不是“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

1 个解决方案

#1


1  

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.

理查德,您应该查看PHP中的json_encode()函数。它会将您的数组快速转换为JSON,并使您不必处理大量数据的JSON语法的细微差别。


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

#1


1  

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.

理查德,您应该查看PHP中的json_encode()函数。它会将您的数组快速转换为JSON,并使您不必处理大量数据的JSON语法的细微差别。


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