在这里,我想在json输出中编码php返回。我很困惑在那里实现。所以,我必须做的正确方式。如何使用json_encode作为基于json的代码返回
的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}'));
?>
会''C-> SITE_URL。$ D-> me-> username'和'$ txtuser'总是一个字符串? – CodeGodie 2015-04-02 18:17:14
删除全部无用 – Zl3n 2015-04-02 18:23:12
输出当我使用PHP作为返回数据是用户名:“http:// localhost/data/johnmike”,个人资料:“约翰” – smile 2015-04-02 18:24:58