在這裏,我想在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