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

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

Expand Messages
  • 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 1 of 3 , Oct 14, 2006
      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

      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.