使用jQuery Ajax解析PHP对象数组

我正在尝试将数据库中的数据添加到jquery中以在网站上呈现.

例如:
使用example.PHP

<?PHP
function testFunction() {

$data = array()
$mydata = new stdClass;
$mydata->example = 'test';
$data[] = $mydata
return json_encode($data);
}

echo testFunction();
?>


的index.html

<script>
$.ajax({
                    type: 'POST',url: 'example.PHP',data: {map: map},cache: false,dataType: 'json',success: function(response) {
                      console.log(response[0].example);
                    }
});

</script>

输出

的console.log(响应);

[“test”,$family:function,$constructor:function,each:function,clone:function,clean:function …]

的console.log(响应[0].实施例);

未定义

基本上,我收到的回复很好,当我记录它时它给了我一个有意义的结构.但是我似乎无法找到访问数组内部对象的正确方法,上面的例子只返回undefined.这请问的正确语法是什么?

解决方法

你需要JSON.parse(响应);响应.你应该能够像你需要的那样访问它..
var parsed_reply = JSON.parse(response);

编辑实际查看代码后:

PHP

<?PHP

$data['example'] = "test";

echo json_encode($data);

?>

JAVASCRIPT

<script>
$.ajax({
                type: 'POST',success: function(response) {
                  console.log(response['example']);
                }
});

</script>

输出:“测试”

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...