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条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题