获取JSON数据从阿贾克斯回调 [英] get json data back from ajax call

查看:137
本文介绍了获取JSON数据从阿贾克斯回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如何让我的PHP脚本发送JSON数据类型 并获得回成功或完整的功能?

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

我试图让 chatfunction工作在我的网站 因为它diddn't工作中,我创建了一个最小化的部分关闭code检查 如果有一些东西做的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.

我只测试了,如果我能得到一个SESSIONNAME回phpscript被proccessed后 我得到的回复是不确定的,而不是JOHNDOE。

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.

这是我的测试code

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

推荐答案

理查,你应该看看到的 json_en code()在PHP函数。它会迅速的数组转换成JSON,并让你不必应付JSON语法的大量数据的较小的细微差别。

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.


更新:修改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"
    		)
    	));
    }
?>

这篇关于获取JSON数据从阿贾克斯回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆