dsfsd43523 2015-05-07 06:47
浏览 32
已采纳

JSON解码不适用于PHP

I have a WAMP server with PHP 5.4.3 version. I encoded the data in JSON by php file below is the code (submit.php).

$username = 'User';
$comment = 'Test comment';
$date = date("Y-m-d G:i:s");  
$image = '/img/sephiroth.png';

  $return = array(
  'username'=> $username,
  'comment' => $comment,
  'date' => $date,
  'image' => $image  );
  echo json_encode($return); 

Now i have a javascript file(script.js).

$.ajax({
            type: "POST",
            url: "submit.php",
            data: dataString,
            cache: false,
            success: function(result){
                    alert(result); //It is showing all the JSON data.
                    $value = result;
                    $data = json_decode($value,true);
                    alert($data);
                        }
           });

I want to decode all the JSON data separately. e.g Username, comment, date, image. So, i can show them on webpage, at the moment all data is coming together. I tried multiple times with multiple options (php array or result.username or result['username'] but no luck.

Now i am getting below error.

ReferenceError: json_decode is not defined
    $data = json_decode($value,true);
  • 写回答

5条回答 默认 最新

  • dtcyv3985 2015-05-07 06:51
    关注

    you cannot use json_decode in JS json_decode is PHP function

    use jQuery.parseJSON(json_data) to parse your json data;

    jQuery.parseJSON( result );
    

    Change

    $value = result;
    $data = json_decode($value,true);
    

    To :

    jQuery.parseJSON( result );
    

    http://api.jquery.com/jQuery.parseJSON/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站