从php访问json数据 [英] Access json data from php

查看:82
本文介绍了从php访问json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问JSON数据时遇到问题。我是JSON和jquery的新手,所以可能有一个简单的解决方案,我很乐意找到它。

I have a problem accessing JSON data. I'm new to JSON and jquery so there is probably a easy solution to it and I would be glad to find out.

我的jQuery:

  $.post(
    "currentPage.php",
    { 
    'currentPage': 1
    },
    function(data){
      $("body").append(data);  
    }
  );

currentPage.php:

currentPage.php:

$returnArray['left'] = 'test_left';
$returnArray['right'] = 'test_right';

$returnArray['numLeft'][] = 1;
$returnArray['numRight'][] = 2;
$returnArray['numRight'][] = 3;

print json_encode($returnArray);

我试图访问这样的数据:

I tried to access the data like this:

data.left
data['left']

但它返回空白,访问HTML文件中数据的最佳方法是什么?

but it returns blank, how is the best way to access the data in the HTML-file?

推荐答案

我可能是错的,但我不认为 post 方法假定数据返回类型为json。您可以通过将ajax函数更改为:

I could be wrong, but I don't think the post method assumes a data return-type of json. You could set that by changing the ajax function to:

  $.post(
    "currentPage.php",
    { 
    'currentPage': 1
    },
    function(data){
      $("body").append(data);  
    },
    "json"
  );

这篇关于从php访问json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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