473,513 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stuffing JSON for an AJAX response

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 11 '07 #1
1 2483
Daz
On May 12, 12:00 am, dan.goye...@gmail.com 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.

May 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4041
by: VK | last post by:
Google Trends is an all new service (started May 10) and I have not responsability for proper query or data accuracy. Overall seems pretty close to what could be observed by the post history in...
3
1669
by: Tom Cole | last post by:
I'm new to JSON but see how it can be an improvement over XML for some things. I've modified a test servlet to return the following string with a content type of text/x-json: { header: {...
1
2275
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
3
2277
by: xhe | last post by:
I found Jason is a very handy data format for client/server communication. But I just met a problem as follows: I want to read the data replied from server in Jason format, the reply is like...
5
1742
by: buntyindia | last post by:
//Outputs: Hi , I am making a application in which I have to send a request using Ajax to ther server and the server return response in JSON . I just wanna know i am getting the following...
1
1892
by: Jeff | last post by:
I'm writing my first json/ajax code and I'm having a hard time wrapping my mind around security issues. I'm thinking of a json response that would look like this: {"data":}...
5
1713
dmjpro
by: dmjpro | last post by:
Recently I came to know that ..... it's better to to generate some JSON instead of plain HTML when i am calling AJAX. But today i read that the usage of eval is dangerous while i processing the AJAX...
0
5366
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
3
6865
Kelicula
by: Kelicula | last post by:
Hi all, I am usually a Perl programmer, I have some background in javascript and am attempting to create a Googleish selector div. Please bear with me, excuse the long introduction... Here's...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.