views:

275

answers:

3

My problem is that this JSON is not being parsed by JavaScript or the jQuery API. I am including the code below and a URL where the JSON output is.

This JSON is being parsed in JavaScript as nothing:

//something like this:
alert(data); // gives an object
alert(data.horas[0].hora; // gives undefined

The JSON itself:

{"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"}]}]}

I've run it in JSONLint and it gives "valid JSON"

You can check it out at:

http://www.my-clock.net/vodafone/getCinema.php?cinemaid=W5

When I call it via $.post or $.get in JSON, I always get empty data in the callback. Can anyone explain to me what it is wrong?


Note:

This is not because of the "same origin policy")


Code:

<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>
+1  A: 

Is it because of the wrong content-type? The URL you gave gives a content-type of "text/html". The correct type is application/json. See this.

[Edit] Are you sure your php handles POST? I just wrote a simple HTML which posted to your page and it returned blank.

Here is the HTML

<html><head>
<body>
    <form method="post" action="http://www.my-clock.net/vodafone/getCinema.php"&gt;
     <input type="text" name="cinemaid" value="W5">
     <input type="submit">
    </form>
</body>
</html>
Chetan Sastry
already added it and it doesn't solve the problem also
fmsf
I've added in the php script header('content-type: application/json'); and it still doesn't solve it
fmsf
I'm just keeping it as text/html to be easier to open with the browser, my localhost version is application/json
fmsf
yeah it does handle post. sry that version online is using get instead of post.
fmsf
+6  A: 

It's because there are line breaks in your strings. If you view the source of the JSON page, you can see them all. If you remove them, the page will work.

Check out:

Also, you can verify your JSON is invalid by navigating to the URL in your post, viewing the source and copying and pasting it into jslint.

seth
yeah you're right, you'll get the correct answer. Just a last question, how do you remove the line breaks in php? the str_replace("\n", "", $string); doesn't seem to be working
fmsf
I've used something line this `$line = ereg_replace("/\n\r|\r\n|\n|\r/", "", $line);`
seth
it works ty. You'll get the correct answer in 2 days
fmsf
Alternatively, if you want to encode your line breaks rather than simply remove them: `$line = str_replace("\n", "\\n", str_replace("\r", "\\r", $line));`
Ben Blank
A: 

Are you properly encoding this in your php script?

It appears that alert(data) returns an object because it is a valid string. alert(data.horas[0].hora gives you undefined because that object isn't being interpreted as JSON (e.g. an array of hora objects each with a hora property).

Try this in your php:

<?php echo json_encode(... your array of hora objects here ...); ?>
wgpubs
if it was only a valid string the alert(data) would output the data.
fmsf