Stuffing JSON for an AJAX response

Discussion in 'Javascript' started by dan.goyette@gmail.com, May 12, 2007.

  1. Guest

    I'm fairly new to using AJAX. I'm currently developing a data grid
    application in coldfusion, using AJAX for paging/filter/sorting
    updates to the grid. So far I've just been returning raw html, and
    updating the innerHTML of a div on the page when the response comes
    back.

    But now I've decided it would be useful to split my response into (at
    least) two parts. The first will be the raw HTML to place in the div.
    The second part will be separate data that will run through a
    javascript function to update some values on the page. The details
    aren't too important, but the idea is that instead of a single block
    of text, I'd like to be able to send two distinct blocks with the ajax
    response.

    In playing around with JSON a bit, I've got it working for a simple
    structure like this:

    {html:"some html",
    javascript:"some javascript to execute"
    }

    I can reference those after evaling the response, and use the text.
    The problem comes when I actually place myt HTML into the JSON object.
    It's kind of a lot of HTML, and something seems to be breaking in the
    response. It seems to be hanging during the eval process on the
    response, though I can't see why.

    Is it a horrible idea to try to pass html within a JSON object? Is
    there a way to encode or escape the text beforehand so that it can be
    passed properly?

    Thanks.
    , May 12, 2007
    #1
    1. Advertising

  2. Daz Guest

    On May 12, 12:00 am, wrote:
    > I'm fairly new to using AJAX. I'm currently developing a data grid
    > application in coldfusion, using AJAX for paging/filter/sorting
    > updates to the grid. So far I've just been returning raw html, and
    > updating the innerHTML of a div on the page when the response comes
    > back.
    >
    > But now I've decided it would be useful to split my response into (at
    > least) two parts. The first will be the raw HTML to place in the div.
    > The second part will be separate data that will run through a
    > javascript function to update some values on the page. The details
    > aren't too important, but the idea is that instead of a single block
    > of text, I'd like to be able to send two distinct blocks with the ajax
    > response.
    >
    > In playing around with JSON a bit, I've got it working for a simple
    > structure like this:
    >
    > {html:"some html",
    > javascript:"some javascript to execute"
    >
    > }
    >
    > I can reference those after evaling the response, and use the text.
    > The problem comes when I actually place myt HTML into the JSON object.
    > It's kind of a lot of HTML, and something seems to be breaking in the
    > response. It seems to be hanging during the eval process on the
    > response, though I can't see why.
    >
    > Is it a horrible idea to try to pass html within a JSON object? Is
    > there a way to encode or escape the text beforehand so that it can be
    > passed properly?
    >
    > Thanks.


    I don't know much about coldfusion, in fact I know next to nothing,
    but I see no reason why you can pass HTML inside of a JSON object.
    After all, what is HTML? It's simply a string of characters. It's how
    you hand it that matters. I guess the preference would be to use XML.
    I have done quite a few projects that start out with a simple HTML
    template (most of which is hidden), ajax is then used to get the data
    to fill it, or get a small amount of HTML to insert. I've never had
    any problems. How are you trying to parse the HTML? I usually use
    innerHTML (which is bad practice, but works for the small jobs I use
    it for). Can you not use innerHTML? It's a little unreliable in the
    sense that if there's an error in the code, it won't appear correctly
    when inserted, but I have never actually evaled the HTML, only the
    JSON object. If this is what you are doing, then you need to make sure
    that you use the correct quotes.

    '<html><head></head><body style="color:blue;"></body></html>' will
    work, whereas "<html><head></head><body style='color:blue;'></body></
    html>" will not.

    You need to make sure that the HTML isn't actually being parsed, by
    making sure it's encapsulated in a string, and any characters that
    need to be escaped, are escaped.

    Hope this helps.

    Daz.
    Daz, May 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    216
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    299
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    229
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    248
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    145
    David Karr
    Jun 17, 2013
Loading...

Share This Page