473,382 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2473
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.