-1

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>
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
fmsf
  • 36,317
  • 49
  • 147
  • 195

3 Answers3

6

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
  • 36,759
  • 7
  • 60
  • 57
  • 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 Jul 29 '09 at 00:45
  • I've used something line this `$line = ereg_replace("/\n\r|\r\n|\n|\r/", "", $line);` – seth Jul 29 '09 at 00:53
  • 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 Aug 01 '09 at 00:35
1

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">
     <input type="text" name="cinemaid" value="W5">
     <input type="submit">
    </form>
</body>
</html>
Community
  • 1
  • 1
Chetan S
  • 23,637
  • 2
  • 63
  • 78
  • I've added in the php script header('content-type: application/json'); and it still doesn't solve it – fmsf Jul 29 '09 at 00:12
  • I'm just keeping it as text/html to be easier to open with the browser, my localhost version is application/json – fmsf Jul 29 '09 at 00:13
  • yeah it does handle post. sry that version online is using get instead of post. – fmsf Jul 29 '09 at 00:34
0

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
  • 8,131
  • 15
  • 62
  • 109