【发布时间】:2009-06-24 07:41:41
【问题描述】:
我的问题是我 解决不了这个问题
如果我调用 php 脚本,我得到的只是一个未定义的错误
这是我用于测试 AND 的代码
this is the original code from the creator that is giving me a headache
function startJsonSession(){
$.ajax({ url: "jsontest.php?action=startjson",
cache: false,
dataType: "json",
complete: function(data) {
username = data.username;
alert(username);
}
});
}
//phpscript
if ($_GET['action'] == "startjson") { startJson(); }
function startJson() {
header('Content-type: application/json');
$items = '';
echo json_encode(array(
"username" => "bob",
"items" => array( "item1" => "sandwich",
"item2" => "applejuice"
)
));
}
谢谢,理查德
编辑了我的问题,因为:
此函数以不同的方式返回 json 数据
因此,下面介绍的解决方案不会产生相同的结果。
function startChatSession() {
$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);
}
【问题讨论】:
-
您是否收到错误消息?
-
你是在动态调用由“action”命名的函数(在 $_POST 中),因为 action==startjson 和 function==startjsonSession()。只是想排除它......
-
我应该澄清一下,你的查询字符串是 url:"jsontest.php?action=startjson" 并且 php 函数是 startjsonSession,所以如果你的 php 处理看起来像 return $_POST['action']( ) 有点事情,那么它就不会调用函数
-
编辑了我的问题,但你的速度更快
-
可能会弄错,但我认为 $.ajax 是 $_POST,所以请尝试查看 $_POST['action'] === "startjson"