Stuffing JSON for an AJAX response

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

  1. dan.goyette

    dan.goyette 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

    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

    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?

    dan.goyette, May 12, 2007
    1. Advertisements

  2. dan.goyette

    Daz Guest

    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, May 12, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.