2012-05-09 38 views
0

私のアプリケーションでは、Uncaught SyntaxErrorが発生します。予期しないトークン:getJSONを使用してサーバーからJSONを取得しているとき。Uncaught SyntaxError:予期せぬトークン::

これは私が使用するjavascriptのである:

$('#employeeListPage').bind('pageinit', function(event) { 
    getEmployeeList(); 
}); 

setInterval ("getEmployeeList()", 10000); 
var vanhadata = ""; 

function getEmployeeList() { 
    $.getJSON(serviceURL + 'getemployees.php?autonumero=' + autonumero + 'callback=?', function(data) { 
     if(JSON.stringify(data) != JSON.stringify(vanhadata)){ 
      $('#employeeList li').remove(); 
      employees = data.key; 
      $.each(employees, function(index, employee) { 
       $('#employeeList').append('<li><a href="keikka.html?id=' + employee.IND + '">' + 
        '<h4>' + employee.OSO + '</h4>' + 
        '<img src="pics/' + employee.TILA + '.png"/>' + 
        '<p>' + employee.AIKA + '</p>' +'</a></li>'); 
     }); 
      $('#employeeList').listview('refresh'); 

      if(vanhadata != "") 
       alert("Uusia keikkoja!");  
      vanhadata = data; 
     } 
    }); 
} 

サーバからのJSONレスポンスは、右のようですが、それはエラーを表示し、何もデータが表示されません。コンソールも出力します。"Resource interpreted as Script but transferred with MIME type text/html:"

これはJSONレスポンスです:

{ 
    "key": [ 
     { 
      "NIMET": "Tuntematon", 
      "PERHJAS": "0", 
      "SAATTAJA": "0", 
      "m_yht": "0", 
      "OSO": null, 
      "AIKA": "2010-03-11 10:00:00", 
      "OSOITELAJI": "0", 

     } 
    ] 
}UncaughtSyntaxError: Unexpectedtoken: 

そしてgetemployees.php:

<?php 
header('Content-Type: application/json; charset=UTF-8'); 

include 'config.php'; 

$number = $_GET['autonumero'] ; 

$sql = "select * from table where number=\"$number\""; 



try { 
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $dbh->query($sql); 
    $employees = $stmt->fetchAll(PDO::FETCH_OBJ); 
    $dbh = null; 
    echo '{"key":'. json_encode($employees) .'}'; 
} catch(PDOException $e) { 
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
} 


?> 

にPHPファイルを変更します。

  if ($_GET['callback']) 
     print $_GET['callback']."("; 

    echo '{"key":'. json_encode($results) .'}'; 

    if ($_GET['callback']) 
     print ")"; 

} catch(PDOException $e) { 
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
} 

応答私は得る:

<br /> 
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'> 
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Parse error: syntax error, unexpected T_CATCH in C:\Services\getemployees.php on line <i>40</i></th></tr> 
</table></font> 

したがって、JSONはまったくありません。予期しないトークンが:ない

+0

[バリデータ](http://jsonlint.com/)でJSONを実行してみてください。また、回答テキストを投稿することはできますか? – Joseph

+0

有効なJSONを出力します。 – user1323294

+0

JSONはInternet Explorerでは無効です。 –

答えて

0

フォースあなたgetemployees.php application/jsonのコンテンツタイプを印刷するには、そのメッセージに:を以下の空欄です。

内部オブジェクトの最後のカンマを削除する必要があります。特に後者のコンマへのオブジェクトのInternet Explorer:コンマの意味は「何か他のものがある」という意味ですが、他のブラウザはカンマを使用して「それはそのものの終わりです」という意味です。

json_encodeを使用しているため、自動的に処理する必要があります。そのため、不正なデータが入力されている可能性があります。クエリから返されたデータを確認します。おそらく空の行がありますか?

+0

私はheader( 'Content-Type:application/json; charset = UTF-8')を持っています。 PHPファイルにあります。それはスクリプトとして解釈されたリソースを削除しますが、MIMEタイプtext/html:partで転送されますが、それでもまだ表示されます Uncaught SyntaxError:予期しないトークン: – user1323294

+0

問題はgetemployees.phpスクリプトであり、javascriptではありません。あなたはそのコードを貼り付けることができますか? –

+0

私は自分の投稿にPHPを編集しました – user1323294

1

代わりにプレーンテキストの

+0

'<?php'と' header'の間のスペースは何もしません。スペースはスペースでなければなりません'<?php'の前に。 –

+0

@GGG。あなたはもちろんそうです。無関係として削除されました。 –

+0

空の行がないようです – user1323294

関連する問題