dtsnx44260 2015-04-02 18:12
浏览 126
已采纳

如何使用json_encode作为基于我的代码的json返回

Here, I want to encode php return in json output.I'm so confused to implement on there. So, how the correct way I have to do.

index.html

    $(function(){
         $.ajax({
            type: 'GET',
            url: "profile.php", 
            success: function(resp){
                var username = JSON.parse(resp).username;
                var profile = JSON.parse(resp).profile;
                $('.test').html(username+profile );

            }
         });
       });

profile.php

<?php
  require_once('class.php');
?>

<?php    
if ($user->is_logged == 1) {
    $txtuser = '';
    if (empty($D->me->firstname)) $txtuser = $D->me->username;
    else $txtuser = $D->me->firstname;

    if (empty($D->me->avatar)) $txtavatar = 'default.jpg';
    else $txtavatar = $D->me->avatar;
}
?>

<?php
 echo json_encode(array('username' => '{$C->SITE_URL.$D->me->username}', 'profile' => '{$txtuser}' ));
?>
  • 写回答

2条回答 默认 最新

  • dqmchw0071 2015-04-02 18:32
    关注

    clean it out this way, and let me know if you still have issues:

    <?php
      require_once('class.php');
    
      if ($user->is_logged == 1) {
        $txtuser = '';
        if (empty($D->me->firstname)) $txtuser = $D->me->username;
        else $txtuser = $D->me->firstname;
    
        if (empty($D->me->avatar)) $txtavatar = 'default.jpg';
        else $txtavatar = $D->me->avatar;
      }
    
      $arr = array(
         "username" => $C->SITE_URL . $D->me->username,
         "profile" => $txtuser
      );
    
      echo json_encode($arr);
    ?>
    

    in your Ajax response use console.log(resp) to see any errors in the console.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog