別のページで完全に機能するコードを含むページでエラーが発生します...これは次のとおりです。
$.each(json,function(index,element){
var tr = $("<tr>").appendTo($tabbody);
$(tr).append('<td><a target="_blank" href="forum.php?t='+element.topic_id+'">'+element.topic_nom+"</a></td>"+'<td>'+element.date_heure+"</td>");
});
jsonは、phpページでのGETリクエストの結果です。
$data = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
結果の例:
[
{
"topic_nom": "Deuxième question de linterro manip 5",
"topic_id": "1",
"user_nick": "Symael",
"user_id": "1",
"msg_id": "10",
"date_heure": "2013-02-17 18:28:04"
},
{
"topic_nom": "Quel est le sens de la vie ?",
"topic_id": "2",
"user_nick": "Symael",
"user_id": "1",
"msg_id": "10",
"date_heure": "2013-02-17 18:28:04"
}
]
どちらが有効なJsonです...私が得るエラーは次のとおりです。
TypeError: invalid 'in' operand e
...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r...
そして、$。parseJSON(json);を試してみると :
SyntaxError: JSON.parse: unexpected character
...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r...
http://jsonlint.com/でJsonを検証すると、結果は次のようになります。
Parse error on line 1:
[ { "to
^
Expecting '{', '['
そして、最初の[を手動で書き直すと、突然有効になります。私はここで非常に奇妙なグリッチの犠牲者だと思います、誰かが私を助けることができますか?Oo
これが解決策です。説明についてはStoiveの回答を参照してください。
obj = $.parseJSON(json.trim());
$.each(obj,function(index,element){
var tr = $("<tr>").appendTo($tabbody);
$(tr).append('<td><a target="_blank" href="forum.php?t='+element.topic_id+'">'+element.topic_nom+"</a></td>"+'<td>'+element.date_heure+"</td>");
});