我目前以这种格式将JSON返回给ajax调用: [ { “用户名”: “TEST3”, “点”:62, “sportName”: “篮球”}, { “用户名”: “TEST2”, “点”:55, “sportName”: “足球”} ]
但我希望这样: { “数据”: [ {“username”:“test3”,“points”:“62”,“sportName”:“Basketball”}, {“username”:“test2”,“points”:“55”,“sportName”:“足球”} ] }
返回它的函数(Laravel):
$scores=DB::select("SELECT user.username, points
FROM scores
JOIN users AS user ON user.id = scores.user_id
ORDER BY points DESC");
return json_encode($scores);
你可以这么做。
return json_encode( ['data' => $scores] );