簡體   English   中英

PHP Json Web服務錯誤

[英]Php Json Web service Error

我用php創建的JSON網絡服務有問題。 當我包含行echo $ _GET ['jsoncallback']。'('。'json_encode($ posts)。')';時,請在JSON中不斷獲取垃圾。 添加到那是在行(某個數字)上得到一個錯誤的未識別索引,但是它回顯$ _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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM