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}' ));
?>

图片转代码服务由CSDN问答提供 功能建议

在这里,我想在json输出中对php返回进行编码。我很难在那里实现。 那么,我必须采取的正确方法。

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); 
 
} 
}); 
}); 
   \  n 
 

profile.php

 &lt;?php 
 require_once('class.php'); 
?&gt; 
 
&lt;?php  
if($ user-&gt; is_logged == 1){
 $ txtuser =''; 
 if(empty($ D-&gt; me-&gt; firstname))$ txtuser = $ D-&gt; me-  &gt;用户名; 
其他$ txtuser = $ D-&gt; me-&gt; firstname; 
 
 if(空($ D-&gt; me-&gt; avatar))$ txtavatar ='default.jpg';  
其他$ txtavatar = $ D-&gt; me-&gt; avatar; 
} 
?&gt; 
 
&lt;?php 
 echo json_encode(array('username'=&gt;'{$ C-  &gt; SITE_URL。$ D-&gt; me-&gt; username}','profile'=&gt;'{$ txtuser}')); 
?&gt; 
   
  
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • douzong6649 2015-04-02 18:58

    Set the dataType option to json, that way you will tell the jQuery that the data expected from the server is in JSON format, and jQuery will try to convert a JSON string into an object. It is not necessary manually do that with JSON.parse().

    $.ajax({
        type: 'GET',
        url: "profile.php",
        dataType: 'json',           
        success: function( resp ){          
            console.log( resp );
        }
    });
    

    Use console.log() to inspect the result ( Mozilla, Chrome ).

    Another thing is that you should remove the quotation marks and just concatenate strings with dot ( PHP String Operators ). Also, there should not be any output before and after json_encode() because it will break json string, su just use die().

    die( json_encode( array( 'username' => $C->SITE_URL . $D->me->username, 'profile' => $txtuser ) ) );
    
    打赏 评论

相关推荐 更多相似问题