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

Re: [json] appropriate use for JSON with AJAX and page updates?

Expand Messages
  • Martin J. Evans
    ... Peter, This is pretty much what we do and it works fine. The only problem we had initially was with numbers and Perl and the 2 different JSON
    Message 1 of 3 , Oct 14, 2006
    • 0 Attachment
      Peter Michaux wrote:
      > Hi,
      >
      > I am working on a new project and I can choose XML, JSON, straight
      > JavaScript or a custom format for transmitting data between browser
      > JavaScript and server Perl. This is for AJAX requests.
      >
      > The client may need to send the position of a couple of user
      > manipulated HTML/CSS popup windows and the server may respond with a
      > few HTML snips to update parts of the web page that want to be
      > updated.
      >
      > I was thinking that JSON might be better because it is lightweight and
      > simple compared to XML.
      >
      > Suppose I construct an object like the following in the browser with
      > JavaScript to be sent to the server
      >
      > var myObject = {
      > newPositions: {window1:[100,324], window2:[83,72]},
      > update: ["my_stock_list", "weather_forecast" ]
      > };
      >
      > window1, window2, my_stock_list and weather_forecast are all unique id
      > attributes of HTML elements.
      >
      > First, is this relatively JSON-ish? I'm trying to aggregate seperate
      > ajax calls into one to reduce server requests and browser activity.
      >
      > Second, how do I send this to the server? Just do
      > myObject.toJSONString() and send the resulting string to the server as
      > the body of a AJAX POST request?
      >
      > The server might use the Perl JSON implementation to reply with the following
      >
      > {
      > update: {my_stock_list: "<li>stock one<\\li><li>stock two<\\li>",
      > weather_forecast: "sunny"}
      > }
      >
      > I could then loop through the properties of "update" where each
      > property corresponds to an HTML element on the page. I could use the
      > non-standard innerHTML on each element to insert the newly returned
      > HTML where it belongs.
      >
      > Does this seem like appropriate use of JSON? Should I even consider
      > any other format like XML?
      >
      > I am really excited abou this project and would greatly appreciate any
      > advice or experience!
      >
      > Thank you,
      > Peter

      Peter,

      This is pretty much what we do and it works fine. The only problem we
      had initially was with numbers and Perl and the 2 different JSON
      implementations in Perl (JSON and JSON::Syck - they don't work exactly
      the same).

      With JSON::Syck:

      $a="1";
      $b="2";
      $c=$a+0;

      @x=($a,$b,$c);

      $js = ObjToJSON(@a);

      results in the approximate JSON:

      [1,"2",1]

      surprisingly I thought. Perl sets the has_been_used_in_number_context
      flag on $a and because JSON::Syck is XS code it can see that so it
      is JSONed as a number. The JSON module, on the other hand, uses a
      regular expression to work out what is a number and what isn't.
      The only impact this has is in your javascript you need to make
      sure what you want to use as numbers is converted to a number when
      you get the data from Perl.

      JSON::Syck is a lot faster than the JSON module.

      Martin
    • Philip Tellis
      ... It is what I would do. To send it to the server, I d pass it as a POST parameter: var content = json= + encodeURIComponent(myObject.toJSONString());
      Message 2 of 3 , Oct 14, 2006
      • 0 Attachment
        Sometime on Oct 13, PM cobbled together some glyphs to say:

        > Suppose I construct an object like the following in the browser with
        > JavaScript to be sent to the server
        >
        > var myObject = {
        > newPositions: {window1:[100,324], window2:[83,72]},
        > update: ["my_stock_list", "weather_forecast" ]
        > };
        >
        > window1, window2, my_stock_list and weather_forecast are all unique id
        > attributes of HTML elements.
        >
        > First, is this relatively JSON-ish? I'm trying to aggregate seperate
        > ajax calls into one to reduce server requests and browser activity.
        >
        > Second, how do I send this to the server? Just do
        > myObject.toJSONString() and send the resulting string to the server as
        > the body of a AJAX POST request?

        It is what I would do. To send it to the server, I'd pass it as a POST
        parameter:

        var content = "json=" + encodeURIComponent(myObject.toJSONString());

        That way you don't have to worry about setting the request content type
        to application/json and telling your server how to handle it. Perl's
        CGI module knows how to deal with url encoded data, so send that in.

        > The server might use the Perl JSON implementation to reply with the following
        >
        > {
        > update: {my_stock_list: "<li>stock one<\\li><li>stock two<\\li>",
        > weather_forecast: "sunny"}
        > }
        >
        > I could then loop through the properties of "update" where each
        > property corresponds to an HTML element on the page. I could use the

        just make sure that you check hasOwnProperty in your for..in loop to
        avoid errors when toJSONString shows up as a key in your response.

        > Does this seem like appropriate use of JSON? Should I even consider
        > any other format like XML?

        Not worth the overhead.


        --
        The basic rule is this: Never support weakness; always support strength.

        -- The Bene Gesserit Azhar Book, Compilation of Great Secrets
      Your message has been successfully submitted and would be delivered to recipients shortly.