我的问题是这个JSON没有被JavaScript或jQuery API解析。我包含下面的代码和JSON输出的URL。
这个JSON在JavaScript中被解析为无:
//something like this:
alert(data); // gives an object
alert(data.horas[0].hora; // gives undefined
JSON本身:
{"horas": [{"hora": "13:20","filmes":[{ "tittle": "A Idade do Gelo 3 - Despertar dos Dinossauros VP3D ", "description": "", "image_url": "images/cartazes/img/a_idade_do_gelo_3_despertar_dos_dinossauros.jpg"}]},{"hora": "15:50","filmes":[{ "tittle": "A Idade do Gelo 3 - Despertar dos Dinossauros VP3D ", "description": "", "image_url": "images/cartazes/img/a_idade_do_gelo_3_despertar_dos_dinossauros.jpg"}]},{"hora": "18:00","filmes":[{ "tittle": "A Idade do Gelo 3 - Despertar dos Dinossauros VP3D ", "description": "", "image_url": "images/cartazes/img/a_idade_do_gelo_3_despertar_dos_dinossauros.jpg"}]},{"hora": "21:05","filmes":[{ "tittle": "A Idade do Gelo 3 - Despertar dos Dinossauros VP3D ", "description": "", "image_url": "images/cartazes/img/a_idade_do_gelo_3_despertar_dos_dinossauros.jpg"}]},{"hora": "13:40","filmes":[{ "tittle": "Igor ", "description": "", "image_url": "images/cartazes/img/igor.jpg"}]},{"hora": "16:10","filmes":[{ "tittle": "Igor ", "description": "", "image_url": "images/cartazes/img/igor.jpg"}]},{"hora": "21:30","filmes":[{ "tittle": "Bruno ", "description": "", "image_url": "images/cartazes/img/bruno.jpg"}]},{"hora": "13:00","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "16:30","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "20:30","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "00:15","filmes":[{ "tittle": "O Barco do Rock ", "description": "", "image_url": "images/cartazes/img/o_barco_do_rock.jpg"}]},{"hora": "12:30","filmes":[{ "tittle": "O Barco do Rock ", "description": "", "image_url": "images/cartazes/img/o_barco_do_rock.jpg"}]},{"hora": "15:25","filmes":[{ "tittle": "O Barco do Rock ", "description": "", "image_url": "images/cartazes/img/o_barco_do_rock.jpg"}]},{"hora": "18:20","filmes":[{ "tittle": "O Barco do Rock ", "description": "", "image_url": "images/cartazes/img/o_barco_do_rock.jpg"}]},{"hora": "00:20","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "13:30","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "17:10","filmes":[{ "tittle": "Harry Potter e o príncipe Misterioso ", "description": "", "image_url": "images/cartazes/img/harry_potter_e_o_principe_misterioso.jpg"}]},{"hora": "13:10","filmes":[{ "tittle": "Cinco Minutos de Paz ", "description": "", "image_url": "images/cartazes/img/cinco_minutos_de_paz.jpg"}]},{"hora": "16:00","filmes":[{ "tittle": "Cinco Minutos de Paz ", "description": "", "image_url": "images/cartazes/img/cinco_minutos_de_paz.jpg"}]},{"hora": "18:30","filmes":[{ "tittle": "Cinco Minutos de Paz ", "description": "", "image_url": "images/cartazes/img/cinco_minutos_de_paz.jpg"}]},{"hora": "21:40","filmes":[{ "tittle": "Cinco Minutos de Paz ", "description": "", "image_url": "images/cartazes/img/cinco_minutos_de_paz.jpg"}]},{"hora": "00:00","filmes":[{ "tittle": "A Proposta ", "description": "", "image_url": "images/cartazes/img/a_proposta.jpg"}]},{"hora": "12:50","filmes":[{ "tittle": "A Proposta ", "description": "", "image_url": "images/cartazes/img/a_proposta.jpg"}]},{"hora": "15:40","filmes":[{ "tittle": "A Proposta ", "description": "", "image_url": "images/cartazes/img/a_proposta.jpg"}]},{"hora": "18:10","filmes":[{ "tittle": "A Proposta ", "description": "", "image_url": "images/cartazes/img/a_proposta.jpg"}]}]}
我在JSONLint中运行它并且它提供了“有效的JSON”
您可以在以下网址查看:
当我在JSON中通过$.post
或$.get
调用它时,我总是在回调中获取空数据。任何人都可以向我解释这是错的吗?
这不是因为“同源政策”)
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$.post("http://localhost/getCinema.php", { cinemaid: 'W5'},
function(data){
alert(data);
alert(data.horas[0]);
}
,"json");
alert("fim");
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:6)
这是因为你的字符串中有换行符。如果您查看JSON页面的源代码,则可以全部查看它们。如果删除它们,该页面将起作用。
退房:
此外,您可以通过导航到帖子中的URL,查看源代码并将其复制并粘贴到jslint来验证您的JSON是否无效。
答案 1 :(得分:1)
是因为错误的内容类型?您提供的URL提供了内容类型“text / html”。正确的类型是application / json。请参阅this。
[编辑] 你确定你的php处理POST吗?我刚写了一个简单的HTML,它贴在你的页面上,然后返回空白。
这是HTML
<html><head>
<body>
<form method="post" action="http://www.my-clock.net/vodafone/getCinema.php">
<input type="text" name="cinemaid" value="W5">
<input type="submit">
</form>
</body>
</html>
答案 2 :(得分:0)
你在php脚本中正确编码吗?
警报(数据)似乎返回一个对象,因为它是一个有效的字符串。 alert(data.horas [0] .hora为您提供了未定义的,因为该对象未被解释为JSON(例如,每个具有hora属性的hora对象数组)。
在你的php中试试这个:
<?php echo json_encode(... your array of hora objects here ...); ?>