<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: What type of data should an Ajax call return?</title>
	<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/</link>
	<description>Brian Burridge shares his thoughts and opinions on Internet Software Architecture and Development, chronicles his current projects and areas of research, and give tips and tricks he discovers along the way.</description>
	<pubDate>Thu, 21 Aug 2008 18:09:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Brian Burridge</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-992</link>
		<pubDate>Thu, 20 Apr 2006 13:55:55 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-992</guid>
					<description>After further consideration I changed my mind. I used a principle of design and architecture that is absolutely correct, but wrongly applied it to the use of AJAX. See my new post on the topic: http://www.brianburridge.com/2006/04/20/ajax-returning-html-change-of-opinion/</description>
		<content:encoded><![CDATA[<p>After further consideration I changed my mind. I used a principle of design and architecture that is absolutely correct, but wrongly applied it to the use of AJAX. See my new post on the topic: <a href='http://www.brianburridge.com/2006/04/20/ajax-returning-html-change-of-opinion/' rel='nofollow'>http://www.brianburridge.com/2006/04/20/ajax-returning-html-change-of-opinion/</a>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Steven Pothoven</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-971</link>
		<pubDate>Wed, 19 Apr 2006 16:21:15 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-971</guid>
					<description>I'm also of the persuasion that XML should be returned over straight HTML.  Using XML allows you to build a richer response allowing the AJAX code to be a little smarter at handling the returned data.

I like the concept (even if I would have made the implementation a little differently) that &lt;a href=&quot;http://www.openrico.org&quot; rel=&quot;nofollow&quot;&gt;Rico&lt;/a&gt; has used to always wrap the response in XML, but use attributes in the XML to indicate what it is.  For example,

&lt;code&gt;
&amp;#60;ajax-response&amp;#62;
&amp;#60;response type=&quot;element&quot; id=&quot;personInfo&quot;&amp;#62;
&amp;#60;div class=&quot;person&quot;&amp;#62;
&amp;#60;span class=&quot;personName&quot;&amp;#62;Mr. Pat Barnes&amp;#60;/span&amp;#62;
&amp;#60;span class=&quot;personAddress&quot;&amp;#62;1743 1st Avenue
Boston, Boston 71204-2345&amp;#60;/span&amp;#62;
&amp;#60;span class=&quot;personOccupation&quot;&amp;#62;Executive Vice
President&amp;#60;/span&amp;#62;
&amp;#60;span class=&quot;personPhoneNumber&quot;&amp;#62;Phone #:
(972) 555-0293&amp;#60;/span&amp;#62;
&amp;#60;span class=&quot;personMobile&quot;&amp;#62;Mobile #:
(972) 555-0295&amp;#60;/span&amp;#62;
&amp;#60;span class=&quot;personNotes&quot;&amp;#62;Notes:
Spouse playes tennis at the country club with
John.&amp;#60;/span&amp;#62;
&amp;#60;/div&amp;#62;
&amp;#60;/response&amp;#62;
&amp;#60;/ajax-response&amp;#62;
&lt;/code&gt;

where the &lt;code&gt;type=&quot;element&quot; id=&quot;personInfo&quot;&lt;/code&gt; indicates to Rico to replace the innerHTML of the element with id &lt;em&gt;personInfo&lt;/em&gt; with the contents of the response.

On the other hand, in a response like:

&lt;code&gt;
&amp;#60;ajax-response&amp;#62;
&amp;#60;response type=&quot;object&quot; id=&quot;formLetterUpdater&quot;&amp;#62;
&amp;#60;person fullName=&quot;Pat Barnes&quot; title=&quot;Mr.&quot; firstName=&quot;Pat&quot;
lastName=&quot;Barnes&quot; streetAddress=&quot;1743 1st Avenue&quot; city=&quot;Boston&quot;
state=&quot;Boston&quot; zipcode=&quot;71204-2345&quot; occupation=&quot;Executive Vice
President&quot; phoneNumber=&quot;(972) 555-0293&quot; mobileNumber=&quot;(972)
555-0295&quot; personNotes=&quot;Spouse playes tennis at the country club
with John.&quot; /&amp;#62;
&amp;#60;/response&amp;#62;
&amp;#60;/ajax-response&amp;#62;
&lt;/code&gt;

The &lt;code&gt;type=&quot;object&quot; id=&quot;formLetterUpdater&quot;&lt;/code&gt; calls a JavaScript object registered under the name matching the id of &lt;em&gt;formLetterUpdater&lt;/em&gt;. That Javascript would then process the contents of the response as XML to extract whatever data is necessary.</description>
		<content:encoded><![CDATA[<p>I&#8217;m also of the persuasion that XML should be returned over straight HTML.  Using XML allows you to build a richer response allowing the AJAX code to be a little smarter at handling the returned data.</p>
<p>I like the concept (even if I would have made the implementation a little differently) that <a href="http://www.openrico.org" rel="nofollow">Rico</a> has used to always wrap the response in XML, but use attributes in the XML to indicate what it is.  For example,</p>
<p><code><br />
&lt;ajax-response&gt;<br />
&lt;response type="element" id="personInfo"&gt;<br />
&lt;div class="person"&gt;<br />
&lt;span class="personName"&gt;Mr. Pat Barnes&lt;/span&gt;<br />
&lt;span class="personAddress"&gt;1743 1st Avenue<br />
Boston, Boston 71204-2345&lt;/span&gt;<br />
&lt;span class="personOccupation"&gt;Executive Vice<br />
President&lt;/span&gt;<br />
&lt;span class="personPhoneNumber"&gt;Phone #:<br />
(972) 555-0293&lt;/span&gt;<br />
&lt;span class="personMobile"&gt;Mobile #:<br />
(972) 555-0295&lt;/span&gt;<br />
&lt;span class="personNotes"&gt;Notes:<br />
Spouse playes tennis at the country club with<br />
John.&lt;/span&gt;<br />
&lt;/div&gt;<br />
&lt;/response&gt;<br />
&lt;/ajax-response&gt;<br />
</code></p>
<p>where the <code>type="element" id="personInfo"</code> indicates to Rico to replace the innerHTML of the element with id <em>personInfo</em> with the contents of the response.</p>
<p>On the other hand, in a response like:</p>
<p><code><br />
&lt;ajax-response&gt;<br />
&lt;response type="object" id="formLetterUpdater"&gt;<br />
&lt;person fullName="Pat Barnes" title="Mr." firstName="Pat"<br />
lastName="Barnes" streetAddress="1743 1st Avenue" city="Boston"<br />
state="Boston" zipcode="71204-2345" occupation="Executive Vice<br />
President" phoneNumber="(972) 555-0293" mobileNumber="(972)<br />
555-0295" personNotes="Spouse playes tennis at the country club<br />
with John." /&gt;<br />
&lt;/response&gt;<br />
&lt;/ajax-response&gt;<br />
</code></p>
<p>The <code>type="object" id="formLetterUpdater"</code> calls a JavaScript object registered under the name matching the id of <em>formLetterUpdater</em>. That Javascript would then process the contents of the response as XML to extract whatever data is necessary.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Brian Burridge</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-927</link>
		<pubDate>Sat, 08 Apr 2006 13:08:01 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-927</guid>
					<description>Thank you for sharing your opinions and methods of using AJAX, Rick. It will make my web site more instructional to have both sides of the debate explained.</description>
		<content:encoded><![CDATA[<p>Thank you for sharing your opinions and methods of using AJAX, Rick. It will make my web site more instructional to have both sides of the debate explained.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Rick Reumann</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-924</link>
		<pubDate>Sat, 08 Apr 2006 06:01:46 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-924</guid>
					<description>Not sure if you can do html quoted text but I'll try...
[quote]Can you explain what you mean by changing select options and form options? Do you mean changing the content of them?[/quote]

Yes, typically changing the content of them, but sometimes it's also changing the value of certain text fields and text areas as well. I use the javascript in a JSP approach for cases where I have several items on the page that all need to change based off of one event firing. Imagine you selected an Employee and you needed to populate a bunch of different Employee fields. Stuff like that.

[quote]Are you saying the part of your application that responds to AJAX requests is returning JSP code that is then executed?[/quote]

It's return a JSP and it's presumed to be javascript that is contained in it, so that's actually what is executed. So for example you can do...

 document.forms[0].endDateString.value = &quot;${endDateString}&quot;;

or basically anything you would typically do with Javascript to set form values or change options. The difference of course being that you can now use information that was set in a servlet that ajax executed.

If was 'just' going to be options in a select list that I needed to change, I could simply return xml in the format 


  ????
 

and use an approriate renderer that the tag package provides.

Personally I like the javascript approach rendered from a JSP since it's 1) easy to develop 2) easy to understand  3) easy to maintain. The third being the biggest advantage. Sometimes I'll get a request that something else must change on the page due to the ajax event, so I basically edit my servlet to make sure I have what I need put into the request, then I just tweak the JSP to make sure the proper javascript is executed. 

So basically, that ajax event fires - calls a servlet - some backend processes usually take place to return data. Servlet puts what it needs into request scope. Servlet returns a JSP which has the javascript in it that I want to execute which is usually like...

//build some javascript string, then...
document.getElementById(&quot;theDivSection&quot;).innerHTML = theStringICreated;
//then several others

If you scroll to the bottom section here http://javawebparts.sourceforge.net/javadocs/javawebparts/taglib/ajaxtags/package-summary.html under Standard Request/Response Handlers you can see the different ways you can see the different handlers. There are a lot of standard ones for typical things, but I've been finding I like the std:CodeExecuter one the best, since if I often need to do more than one thing based off the ajax event, it's easy for me to do with javascript. Like in this one stupid app I have, there are bunch of fields and other things that need to change when a single option is selected (it really should be a desktop application and not a webapp imo and hopefully I'll get to write it as such.)</description>
		<content:encoded><![CDATA[<p>Not sure if you can do html quoted text but I&#8217;ll try&#8230;<br />
[quote]Can you explain what you mean by changing select options and form options? Do you mean changing the content of them?[/quote]</p>
<p>Yes, typically changing the content of them, but sometimes it&#8217;s also changing the value of certain text fields and text areas as well. I use the javascript in a JSP approach for cases where I have several items on the page that all need to change based off of one event firing. Imagine you selected an Employee and you needed to populate a bunch of different Employee fields. Stuff like that.</p>
<p>[quote]Are you saying the part of your application that responds to AJAX requests is returning JSP code that is then executed?[/quote]</p>
<p>It&#8217;s return a JSP and it&#8217;s presumed to be javascript that is contained in it, so that&#8217;s actually what is executed. So for example you can do&#8230;</p>
<p> document.forms[0].endDateString.value = &#8220;${endDateString}&#8221;;</p>
<p>or basically anything you would typically do with Javascript to set form values or change options. The difference of course being that you can now use information that was set in a servlet that ajax executed.</p>
<p>If was &#8216;just&#8217; going to be options in a select list that I needed to change, I could simply return xml in the format </p>
<p>  ????</p>
<p>and use an approriate renderer that the tag package provides.</p>
<p>Personally I like the javascript approach rendered from a JSP since it&#8217;s 1) easy to develop 2) easy to understand  3) easy to maintain. The third being the biggest advantage. Sometimes I&#8217;ll get a request that something else must change on the page due to the ajax event, so I basically edit my servlet to make sure I have what I need put into the request, then I just tweak the JSP to make sure the proper javascript is executed. </p>
<p>So basically, that ajax event fires - calls a servlet - some backend processes usually take place to return data. Servlet puts what it needs into request scope. Servlet returns a JSP which has the javascript in it that I want to execute which is usually like&#8230;</p>
<p>//build some javascript string, then&#8230;<br />
document.getElementById(&#8221;theDivSection&#8221;).innerHTML = theStringICreated;<br />
//then several others</p>
<p>If you scroll to the bottom section here <a href='http://javawebparts.sourceforge.net/javadocs/javawebparts/taglib/ajaxtags/package-summary.html' rel='nofollow'>http://javawebparts.sourceforge.net/javadocs/javawebparts/taglib/ajaxtags/package-summary.html</a> under Standard Request/Response Handlers you can see the different ways you can see the different handlers. There are a lot of standard ones for typical things, but I&#8217;ve been finding I like the std:CodeExecuter one the best, since if I often need to do more than one thing based off the ajax event, it&#8217;s easy for me to do with javascript. Like in this one stupid app I have, there are bunch of fields and other things that need to change when a single option is selected (it really should be a desktop application and not a webapp imo and hopefully I&#8217;ll get to write it as such.)
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Brian Burridge</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-917</link>
		<pubDate>Fri, 07 Apr 2006 21:58:39 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-917</guid>
					<description>Can you explain what you mean by changing select options and form options? Do you mean changing the content of them?

Are you saying the part of your application that responds to AJAX requests is returning JSP code that is then executed?

Do you prefer this method because of ease of development, ease of maintenance, processing time (ie. wait time for the user), or another reason?

Brian</description>
		<content:encoded><![CDATA[<p>Can you explain what you mean by changing select options and form options? Do you mean changing the content of them?</p>
<p>Are you saying the part of your application that responds to AJAX requests is returning JSP code that is then executed?</p>
<p>Do you prefer this method because of ease of development, ease of maintenance, processing time (ie. wait time for the user), or another reason?</p>
<p>Brian
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Rick Reumann</title>
		<link>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-915</link>
		<pubDate>Fri, 07 Apr 2006 19:32:23 +0000</pubDate>
		<guid>http://www.brianburridge.com/2006/01/30/what-type-of-data-should-an-ajax-call-return/#comment-915</guid>
					<description>I think what you return depends on the situation. If I am going to be returning data that is typically going to be used for display purposes (orders, lists of persons, etc) XML makes sense. But for my typical use of Ajax - changing the content of html select options, I don't bother returning XML. My servlet that is called does it's thing (puts what it needs into Request scope) then returns a JSP. The Java Web Parts Ajax tags I use http://javawebparts.sourceforge.net/ make it all very simple. Then in my JSP, I use JSTL to generate the html that I need within JavaScript to replace the divs I need (set element's innerHTML with my generated javascript).

I see no point in returning XML if I'm just going to be changing stuff like select options or other html form options. You have separation of concerns since you are dealing with display in a totally different area (jsps) (The generated code isn't being done in the calling servlet).</description>
		<content:encoded><![CDATA[<p>I think what you return depends on the situation. If I am going to be returning data that is typically going to be used for display purposes (orders, lists of persons, etc) XML makes sense. But for my typical use of Ajax - changing the content of html select options, I don&#8217;t bother returning XML. My servlet that is called does it&#8217;s thing (puts what it needs into Request scope) then returns a JSP. The Java Web Parts Ajax tags I use <a href='http://javawebparts.sourceforge.net/' rel='nofollow'>http://javawebparts.sourceforge.net/</a> make it all very simple. Then in my JSP, I use JSTL to generate the html that I need within JavaScript to replace the divs I need (set element&#8217;s innerHTML with my generated javascript).</p>
<p>I see no point in returning XML if I&#8217;m just going to be changing stuff like select options or other html form options. You have separation of concerns since you are dealing with display in a totally different area (jsps) (The generated code isn&#8217;t being done in the calling servlet).
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
