Php Json Web服务错误

Php Json Web服务错误,php,json,Php,Json,我在使用php创建JSON web服务时遇到问题。当我包含echo$_GET['jsoncallback'].('JSON_encode($posts)。')行时,在我的JSON中不断获取垃圾;除此之外,我在第行(一些数字)上得到了一个无法识别的错误索引,但这是echo$_GET['jsoncallback']。('json_encode($posts)。');出现了 如果我不使用这一行,我会得到JSON,但最终会出现无效标签的错误 我如何解决这个问题?我的代码如下 <?php $link

我在使用php创建JSON web服务时遇到问题。当我包含echo$_GET['jsoncallback'].('JSON_encode($posts)。')行时,在我的JSON中不断获取垃圾;除此之外,我在第行(一些数字)上得到了一个无法识别的错误索引,但这是echo$_GET['jsoncallback']。('json_encode($posts)。');出现了

如果我不使用这一行,我会得到JSON,但最终会出现无效标签的错误

我如何解决这个问题?我的代码如下

<?php
$link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
mysql_select_db('music_db',$link) or die('Cannot select the DB');

/* grab the posts from the db */
$query = "SELECT track.track_id, track.track_name, artist.artist_name, genre.genre_name FROM artist INNER JOIN (genre INNER JOIN track ON genre.genre_id = track.genre_id) ON artist.artist_id = track.artist_id";
$result = mysql_query($query,$link) or die('Errant query:  '.$query);

/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
  $posts[] = array('post'=>$post);
 }
}

/* output in necessary format */
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
@mysql_close($link);
?>

您收到通知,因为您尝试使用的索引未设置。你需要在使用前检查一下

if(isset($_GET['jsoncallback'])) {
  // Code here
}

在输出此
$\u GET['jsoncallback'].('.json_encode($posts.'))之前使用检查如果未设置,则返回直接输出json。因此,您要说的是如果(isset($\u GET['jsoncallback']){$\u GET['jsoncallback'])。('.json_encode($posts)。');}否则{我在这里放什么?}
您可以在不预先设置回调函数的情况下回显json输出。好的,我不明白您的意思,您能举个例子吗