Skip to main content
Inspiring
April 18, 2007
Answered

Using AJAX with ColdFusion...

  • April 18, 2007
  • 2 replies
  • 810 views
Is it true that AJAX doesn't really fetch data realtime from the database when they say they do? For example, I have 2 lists of products that I display using AJAX (See code below). On Template A, if I click the "Display Top 5 Products" link and then click the "Display Top 10 Products" link and then leave the browser there, then I go to my database and modify the content of the second Product Name in the top 5 products, then click on the "Display Top 5 Products" link again, I don't see the modified content in the second Product Name. Is AJAX supposed to only display different sets of pre-loaded data?

Template A:
<html>
<head>
<title>Untitled</title>
<script language="javascript" type="text/javascript">
function createRequestObject(){
var obj;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer"){
obj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
obj = new XMLHttpRequest();
}
return obj;
}

var http = createRequestObject();

function outputResult(c) {
http.open("get", "TemplateB.cfm?Count=" + c, true);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if (http.readyState == 4) {
document.getElementById('result').innerHTML = http.responseText;
}
}
</script>
</head>

<body>
<a href="javascript:outputResult(5)">Display Top 5 Products</a><br>
<a href="javascript:outputResult(10)">Display Top 10 Products</a><br>
<br><br>
<div id="result"></div>

</body>
</html>

Template B (TemplateB.cfm):
<cfquery name="GetProducts" datasource="MyDataSource">
SELECT TOP #Count# Product_ID, Product_Name, Product_Desc
FROM Products
ORDER BY Product_ID DESC
</cfquery>

<html>
<head>
<title>Untitled</title>
</head>

<body>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>Product ID</td>
<td>Product Name</td>
<td>Product Description</td>
</tr>
<cfoutput query="GetProducts">
<tr>
<td>#Product_ID#</td>
<td>#Product_Name#</td>
<td>#Product_Desc#</td>
</tr>
</cfoutput>
</table>

</body>
</html>


This topic has been closed for replies.
Correct answer maxell
quote:

Is it true that AJAX doesn't really fetch data realtime from the database when they say they do?

I'm not sure who "they" are, but no that's not true. Your request is probably getting cached when you first test it. Try adding a random variable to your query string in your asynchronous get request. Using a date-time var is the easiest way to do this.

2 replies

Inspiring
April 18, 2007
Is it true that AJAX doesn't really fetch data realtime from the
database when they say they do?

I wanted to add a comment about this concerning the concept of pulling
and pushing data. Your example was an example of pulling the data. But
the above quote may have been referring to the ability to push data.

AJAX is only capable of pulling data. That means if you change the
database, any client user interface that had already received the data
will not be updated until that client requests new data through some
type of event. A great deal of the time this is good enough, especially
since the event could be scheduled to occur any given time period.

But if one has the full Flex, with the FDS "Flex Data Services" piece
then one is able to push data. It would then be possible to have the
data update trigger an event to push the new data out to all open
clients without any action on their end.

Albeit the FDS piece is the truly pricey part of Flex, when one truly
needs to push data, one needs to push it! Simulating it with scheduled
pulls may not cut it.
Inspiring
April 21, 2007
Hi Ian Skinner,

Yes, my intention is to pull data (real-time) whenever user clicks (an event occurs) on the link again as mentioned in my original post. No, I don't need the server to automatically update my client's data if the data changes in the db and if the browser is left open and untouched.

Anyway, I thank you very much for your comment though. I've just learned something new again.

maxellCorrect answer
Participating Frequently
April 18, 2007
quote:

Is it true that AJAX doesn't really fetch data realtime from the database when they say they do?

I'm not sure who "they" are, but no that's not true. Your request is probably getting cached when you first test it. Try adding a random variable to your query string in your asynchronous get request. Using a date-time var is the easiest way to do this.
Participating Frequently
April 18, 2007
Also you shouldn't be requesting a complete html page since you are only appending an element of of your requesting page. Remove the body tags and everything outside of them on TemplateB.cfm