Loading ...
Sorry, an error occurred while loading the content.

374Re:[json] My javascript app freezes at eval'uating JSON string returned

Expand Messages
  • rancioadams
    May 28, 2006
    • 0 Attachment
      Hi Fang,

      I really appreciate your efforts but I tried that too and it didn't
      work (as a matter of fact, I believe the parenthesis are just a
      "safety measure").

      Now I've been able to solve the problem and what's happening is hard
      to believe for me but I've been testing it thoroughly this afternoon.

      This is what's going on:

      The PHP part is sending a string with 67 characters in front and 2
      characters at the end. IE, instead of the expected string, I get
      something like this:

      garbage(67 char) + string + garbage(67 char)

      I don't have a clue of what produces these characters or what they
      content, but after much testing with string.length and
      string.substring, I came to the conclusion that I have to pass the
      data through the following filter:

      var datos = xmlhttp.responseText;

      datos = datos.substring(67,datos.length-2);

      If I do that, I get the proper string that will eval'uate properly.
      This has nothing to do with the JSON.php stuff. Even if I don't use
      JSON at all, I stil get all that garbage in the same exact numbers.

      Do you have any idea of what's going on here?

      I'm working on UTF-8 all the time, but I don't think this should make
      the difference, does it?

      I'll leave it like that (since it's working), but I would really want
      to know what are all those garbage characters, who puts them there and
      what are they intended for or if there's a bug somewhere that can be
      causing that.

      Regards,

      Pablo



      --- In json@yahoogroups.com, Fang Yidong <fangyidong@...> wrote:
      >
      >
      > I know what the problem is now.The line in your client
      > code:
      >
      > var tabla = eval(xmlhttp.responseText);
      >
      > should be:
      >
      > var tabla = eval("("+xmlhttp.responseText+")");
      >
      >
      >
      > --- rancioadams <comomolo@...>:
      >
      > > Thanks but no, there's not a CR between "GENERAL DE"
      > > and "OBRA". Just
      > > some problem with the posting here.
      > >
      > > Do the nulls at the end of the string pose a problem
      > > to the encoder?
      > >
      > > I'm using the enconder found here:
      > >
      > http://pear.php.net/pepr/pepr-proposal-show.php?id=198
      > >
      > > Regards.
      > >
      > > p
      > >
      > > --- In json@yahoogroups.com, Fang Yidong
      > > <fangyidong@> wrote:
      > > >
      > > >
      > > > --- rancioadams <comomolo@>:
      > > >
      > > > > BTW: Here's the string returned by JSON, just in
      > > > > case:
      > > > >
      > > > > ["160","GENERAL DE
      > > > >
      > > >
      > >
      >
      OBRA","HORMIGONERAS","REF.","TIPO","CORRIENTE","P\/HORA","LITRO","","","","","EURO\/DIA","SEG.\/DIA",null,null,null,null]
      > > > >
      > > >
      > > > It seems that there's a CR or LN between 'GENERAL
      > > DE'
      > > > and 'OBRA' in the result. In JSON , controls
      > > should be
      > > > escaped, and CR should be escaped as '\r' and LN
      > > > should be escaped as '\n'.Choose a correct
      > > > implementation of JSON encoder.
      > > >
      > > >
      > > > > p
      > > > >
      > > > > --- In json@yahoogroups.com, "rancioadams"
      > > > > <comomolo@> wrote:
      > > > > >
      > > > > > Here's the server code:
      > > > > > <meta http-equiv="content-type"
      > > > > content="text/html;charset=utf-8">
      > > > > > <?php
      > > > > >
      > > > > > require_once('JSON.php');
      > > > > > $json = new Services_JSON();
      > > > > >
      > > > > > $con=mysql_connect("xxxxx", "xxxxx", "xxxxx");
      > > > > >
      > > > > > if (!$con)
      > > > > > {
      > > > > > die('Could not connect: ' . mysql_error());
      > > > > > }
      > > > > > mysql_select_db("xxxxx", $con);
      > > > > >
      > > > > > $subfamilia =
      > > utf8_decode($_GET["subfamilia"]);
      > > > > >
      > > > > > $sql = "SELECT * FROM equipos WHERE SUBFAMILIA
      > > =
      > > > > '$subfamilia'";
      > > > > >
      > > > > > $query = mysql_query($sql) or
      > > die(mysql_error());
      > > > > >
      > > > > > $row = mysql_fetch_row($query);
      > > > > > echo $json->encode($row);
      > > > > >
      > > > > > ?>
      > > > > >
      > > > > > And here's the client code:
      > > > > >
      > > > > > function getLista(subfamilia)
      > > > > > {
      > > > > > var xmlhttp=false;
      > > > > >
      > > > > > try
      > > > > > {
      > > > > > xmlhttp = new
      > > > > ActiveXObject('Msxml2.XMLHTTP');
      > > > > > }
      > > > > >
      > > > > > catch (e)
      > > > > > {
      > > > > > try
      > > > > > {
      > > > > > xmlhttp = new
      > > > > ActiveXObject('Microsoft.XMLHTTP');
      > > > > > }
      > > > > > catch (E)
      > > > > > {
      > > > > > xmlhttp = false;
      > > > > > }
      > > > > > }
      > > > > >
      > > > > > if (!xmlhttp && typeof
      > > > > XMLHttpRequest!='undefined')
      > > > > > {
      > > > > > xmlhttp = new XMLHttpRequest();
      > > > > > }
      > > > > >
      > > > > > var url =
      > > 'scripts/getLista.php?subfamilia=' +
      > > > > > encodeURIComponent(subfamilia);
      > > > > >
      > > > > > xmlhttp.open('GET', url, true);
      > > > > >
      > > > > > xmlhttp.onreadystatechange=function()
      > > > > > {
      > > > > > switch (xmlhttp.readyState)
      > > > > > {
      > > > > > case 1:
      > > > > >
      > > > > document.getElementById('lista').innerHTML =
      > > > > "Loading...";
      > > > > > break;
      > > > > >
      > > > > > case 4:
      > > > > > document.write("before <br />");
      > > > > >
      > > > > > document.write(xmlhttp.responseText +
      > > > > "<br />");
      > > > > >
      > > > > > var tabla =
      > > eval(xmlhttp.responseText);
      > > > > > document.write(tabla[2] + "<br />");
      > > > > >
      > > > > > document.write("after <br />");
      > > > > >
      > > > > > break;
      > > > > > }
      > > > > > }
      > > > > > xmlhttp.send(null)
      > > > > > return;
      > > > > > }
      > > > > >
      > > > > > The behaviour is this:
      > > > > > - If I comment the eval line, the I can see
      > > the
      > > > > "after" and "before",
      > > > > > as well as the string returned by JSON from
      > > the
      > > > > PHP code.
      > > > > >
      > > > > > - If I un-comment the string, I'll see "after"
      > > and
      > > > > ths JSON string,
      > > > > > but the app will freeze after that.
      > > > > >
      > > > > > Thanks for any help.
      > > > > >
      > > > > > Pablo
      > > > > >
      > > > > >
      > > > > > --- In json@yahoogroups.com, Fang Yidong
      > > > > <fangyidong@> wrote:
      > > > > > >
      > > > > > >
      > > > > > > Show your source code of client side and
      > > server
      > > > > side
      > > > > > > to us, and maybe we can figure out what the
      > > > > problem
      > > > > > > is.
      > > > > > >
      > > > > > > --- rancioadams <comomolo@>:
      > > > > > >
      > > > > > > > Hi everyone. I'm new to this group so I'd
      > > like
      > > > > to
      > > > > > > > say hello. My name
      > > > > > > > is Pablo, despite of the dumb name in my
      > > > > email.
      > > > > > > >
      > > > > > > > My little AJAX app doesn't like the JSON
      > > > > encoded
      > > > > > > > strings returned from
      > > > > > > > the PHP part of the app. I mean, the PHP
      > > code
      > > > > > > > returns an apparently
      > > > > > > > correct string and I can see the data if I
      > > put
      > > > > the
      > > > > > > > JSON string
      > > > > > > > directly into the div element, but my
      > > > > javascript
      > > > > > > > hangs if I try to
      > > > > > > > eval the string. If I create an
      > > "identical"
      > > > > string
      > > > > > > > manually, my
      > > > > > > > javascript evaluates it correctly. This
      > > > > suggests the
      > > > > > > > JSON string
      > > > > > > > contains unprintable non-eval'able
      > > characters.
      > > > > I'm
      > >
      > === message truncated ===
      >
      >
      >
      > ___________________________________________________________
      > JSON: Action in AJAX!
      >
      > JSON - http://www.json.org
      > JSON.simple - http://www.json.org/java/simple.txt
      >
      >
      > __________________________________________________
      > ¸Ï¿ì×¢²áÑÅ»¢³¬´óÈÝÁ¿Ãâ·ÑÓÊÏä?
      > http://cn.mail.yahoo.com
      >
    • Show all 13 messages in this topic