473,671 Members | 2,298 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:"som e 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 2500
Daz
On May 12, 12:00 am, dan.goye...@gma il.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:"som e 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:bl ue;"></body></html>' will
work, whereas "<html><hea d></head><body style='color:bl ue;'></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
4051
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 c.l.j. Just curious why exactly Japan got so exclusively hot on JSON ? <http://www.google.com/trends?q=AJAX+JavaScript&ctab=0&geo=all&date=all> <http://www.google.com/trends?q=JSON+JavaScript&ctab=0&geo=all&date=all>
3
1678
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: { date: "16 Aug 2006 19:53:03 GMT", headers: } } On the client side I receive this an create an object via eval:
1
2291
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 would like to read the JSON string sent from the client on the
3
2291
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 this: it is generated automatically by amfphp1.9 from an array. I used Ajax to call a method, and the server code replied an array in the above
5
1751
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 response from the server: {"length":50, "accounting":, "sales":}
1
1907
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":} ,{"name":"name2","street":"street2"} ], "instructions":{"function_to_execute":"some_function"} }
5
1717
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 JSON response suing eval. I could not get the point ....Could you please explain me!
0
5370
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 you an example of how an data class (actually, a list of PHP documentation pages) might be represented, first in XML. and then in JSON. This side-by-side comparison should let you begin to understand how to represent data in JSON. The XML version:...
3
6886
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 the details: Our site filters content by location. I use zip codes to find radius's. So if a user wants to see content from a location that they don't know the zip code for, I have set up this neat little input field that allows you to type the...
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.