[英]Returning json data from php to ajax
我正在尝试从php获取一个json对象,所以我可以在ajax中使用它。 这是我的ajax代码:
$.ajax({
type: 'get',
url: eventsListPath,
dataType : "json",
data: {},
success: function (data) {
$('#eventInformation').html(data.table);
alert(data.table);
}
});
我的PHP:
$obj->table="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);
但行
alert(data.table);
以'undefined'回来。 有任何想法吗?
在你的PHP代码中试试这个。 Json编码一个数组。
$obj['table']="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);
替代 - 或者你的班级应该是这样的
class your_classname
{
public $table;
//other class related code
}
$obj = new your_classname;
$obj->table="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);
如果我没弄错的话,json_encode只适用于数组
$obj = [{table:"hey"}];
<?php
$obj = new stdClass();
$obj->table="hey";
echo json_encode($obj)
产生
{ “表”: “哎”}
使用Firebug进行检查。 另外检查内容类型,应该是Content-Type: application/json
你必须将数组传递给json_encode而不是对象
<?php
$array['table'] = "hey";
echo json_encode($array, JSON_UNESCAPED_SLASHES);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.