As my last act before heading out for vacation (ok, I lie, you know I'm going to blog again, at least 5-6 times), here is an incredibly simple jQuery example involving ColdFusion on the back end. This is not new. But I had a reader write in looking for a very specific example, and I couldn't find a simple blog entry of mine to meet his needs. Basically:
- Click a button
- Tell the user the slow process is begun
- Fire off an Ajax request to the server
- Show the response
I've done this in most all of my Ajax demos, but a quick little example couldn't hurt. Here is the front end.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#buttonTest").click(function() {
//cache the status div
var status = $("#status");
//First, do a statuc message
status.html("<i>Loading awesome. Please stand by.</i>");
//Now hit our slow process...
$.get("slow.cfm", {}, function(res,code) {
//Assume the result is basic HTML, so just show it
status.html(res);
});
});
});
</script>
</head>
<body>
<input type="button" id="buttonTest" value="Push for Love">
<div id="status"></div>
The logic is rather simple. On clicking a button, update a div to tell the user what is going on, then fire off the XHR request. Normally I'd hit a CFC and work with complex results, but in this case we are assuming a nicely formatted result that can be dumped into the div.
Our server side code....
<cfset sleep(2000)>
<cfoutput>Hello from awesome. #randRange(1,100)#</cfoutput>
The sleep command there is simply to help simulate a slow process.
And that's it. Sorry if this bores folks, but even when I do super simple examples for readers, I've got to turn it into a blog entry. :)
Archived Comments
Just curious. I tried hitting the button twice, but the script was on fired the first time.
..Sorry, It was only an issue in Internet Explorer 8. OK in other browswers.
Here was a good post from Dan G. Switzer:
http://blog.pengoworks.com/...
And here's an example I did a while back with error trapping:
http://www.phillipsenn.com/...
It has source code at the bottom and a link to a copy of the cfc.
Lots of little tricks I've picked up from you, including adding <cfparam name="url.returnformat" default="json">
and queryFormat=column.
Rich - one more thing I could do is disable the button while the operation is running. That way it won't fire off multiple times at once if you click quickly.
Hi Ray. FYI. When I said I hit the button twice, I meant the 2nd time was after the response was received. In IE8, this was an issue.
That is truly odd. I can see of no reason why IE8 would fail in such a simple demo. Of course, I can't test this since I have IE9. If you can figure out the exact reason, I'd appreciate it.
Hey Ray,
If you don't know (unless you are a compatibility mode hater), just hit f12 and select compatibility mode for ie8. I would say it's about 99.975% correct. Cheers/Merry Xmas and I appreciate all of your blogs.
-Chris
Holy crap - you aren't kidding. It does break. But why?? Time to dig.
Ohhhhhh I got it - I see the network call is running N times (thanks IE for adding that), but it is a caching issue.
Any IE users having issues can run:
http://www.raymondcamden.co...
The only change was this:
$.ajaxSetup ({
// Disable caching of ANY AJAX responses.
cache: false
});
Kinda ridiculous how easy that is in jQuery.
I think I need to update my example to show the new deferred .done and .fail hotness. One problem with some of this stuff is there are multiple "right" ways to do things. But I think .done and .fail are to be used instead of anonymous functions in the callback parameters.
Not sure I'd argue that would be 'better' in this example (of course, better is very much a point of view). Not sure it makes sense in such a simple use case of, "click, get crap", type operations.
That being said - I'm still trying to wrap my brain around them - so I'd welcome seeing your change.
OK, I've updated it. Even though it's working, I'm still not sure if this is "the right" way to use .done and .fail because it just looks like I'm substituting words for .success and .error.
There's a good Google doc here: https://docs.google.com/pre...
It was in response to my StackOverflow question here:
http://stackoverflow.com/qu...
Quick note - you had quite a few typos for function - you spelled it as funciton. I do that a _lot_. This is a nice little preso... and it helps a bit (I mean helps me a bit). By any chance could you also make real file examples of that for folks to download and run?
And to be clear - me asking for files is being lazy. I get that. ;)
The Google doc was from someone else.
He lost me about 3/4 of the way through.
hi Ray, i am trying to implement News slider starting with a static one initially so when I am trying it using the Coda slider.i think I am getting preload error because it shows me loading and a broken pic. please help me out.
Um... no idea what you are talking about. If you want to ping me via my contact form with details, it may make more sense. It will also help if you have code I can look so I can see the issue directly.