I’ve just released an update to GORM Labs and JQuery PeriodicalUpdater (which was introduced in this post). While the modifications to GORM Labs were improvements, the JQuery PeriodicalUpdater update ended up being a punt on some functionality which I thought was successfully implemented.
Specifically, the problem with JQuery PeriodicalUpdater is that I can’t seem to get at the text of the response via JQuery’s $.ajax callbacks. I’d like to store the response text and then check it against the previous response text, and only trigger the success callback if there was a change in that text. This is a requirement to deal with web APIs that oh-so-often completely neglect to send Not Modified if the result hasn’t been modified. If someone has some code that works for getting at the text of the response, I’d love to see it. And yes, I know about xhr.responseText and xhr.responseXML: the XHR isn’t directly accessible in the success callback, and this.xhr().responseText always gives the empty string and this.xhr().responseXML always gives undefined.
Details about the GORM Labs update can be found here and here. As always, the GORM Labs documentation is up-to-date. I’ve also shifted GORM Labs from WTFPL (popularized in Dear User of My Open Source Project) to CC0. I’m hoping that will make people (and their lawyers) a bit more comfortable.
BTW, I’ve gotten precisely zero feedback on my Cry for Help on Open Source Projects. Which is a bummer.
Comments
- September 27, 2009, Bill wrote: I haven't looked at your code but could you check the response text at the "complete" event if the status=200? I'm not sure if complete happens before success is raised but perhaps, if is is you could handle it this way
- September 27, 2009, Robert Fischer wrote: Unfortunately,
completeis called aftersuccess. And it also isn't passed the parsed data, so I can't just pick up all my logic and drop it intocomplete. I also tried thedataFiltercallback, but 1) it's only in the newest jQuery, which hinders adoption; 2) thedatapassed in there also doesn't seem to be useful; and 3) thetypeparameter there isn't set to the inferred types (*.xml), which is annoying. - September 27, 2009, Robert Fischer wrote: If I could get useful data out of
complete, I suppose I could grab the parsed data insuccess, store it into an enclosed variable, and then usecompletecallback to check the data for duplication and only run the user-supplied callback if there's a change... BTW, I've also tried leveraging the to-params function in jQuery and a variety of other serialization stunts, but none of them work particularly well. - September 28, 2009, Bill wrote: I just tried this:
function submitMyForm(){ $.ajax({ type: "GET", url: this.action, success: function(html){ console.log(html); }, complete: function(htr){ console.log(htr); return false; if(htr.status == 200){ console.log(htr); } } }); return false; };and the "complete" method does return to me the xmlHttpRequest object so I can fetch the responseText etc.. I'm not sure if this will help you at all - it was just a weak proof of concept. There is also this option: http://docs.jquery.com/Ajax/ajaxSuccess#callback Which I think gets executed everything an ajax requiest is completed successfully.. It has a method signature like so:function (event, XMLHttpRequest, ajaxOptions) { this; // dom element listening }Hope this helps Bill - September 28, 2009, Robert Fischer wrote:
ajaxSuccesshappens aftersuccessis executed, so that's no good. In the first case, checkhtr.responseText: that was returning""for me every time. Andhtr.responseXMLwas returningundef. - September 28, 2009, Bill wrote: Hrm, weird.. I put up a very rough demo here: http://oak.sbcs.com/demo/jqpost.html that shows that my "complete" is getting back an htr object.. You need firebug enabled on the panel to see the results since I'm using console.log()
- September 28, 2009, Robert Fischer wrote: WTF? That's working for me, too. This doesn't, though: http://demo.smokejumperit.com/demo.html
- September 29, 2009, Bill wrote: I dunno. I am using an older version of JQuery even (1.2.6) I think htr.responseXML will return null unless your response header indicates a content type of XML (http://www.w3.org/TR/XMLHttpRequest/#response-entity-body0). Since you are returning JSON I think responseXML will always be null. Scratch that - now I see you can optionally have it return XML so you probably already tested it that way as well. Now I realize I'm just bastardizing your code here but perhaps this will help:
var returnData; ajaxSettings.success = function(data){ returnData = data; }; ajaxSettings.complete = function(xhr,success) { if(success ==='success'){ var data = xhr.responseText.trim(); if(settings.success) { settings.success(returnData); } timerInterval = settings.minTimeout; if(callback) callback(returnData); returnData = ''; } };So basically I capture the parsed data in success to be returned in complete.. (this handles json parsing/xml, all that stuff that jquery does automatically) and gives you access to both it and the xhr.. However, I don't really notice this periodically updating after the inital page load (I copied your source.json and manually changed it while watching the page.. In fact just to see if it would periodically run I updated getdata to this:function getdata() {console.log('running'); $.ajax(ajaxSettings); }to show me that it was going to do it's thing.. but I only ever see "running" logged one time. Anyway, hope this helps - September 29, 2009, Robert Fischer wrote: Yeah, I bet I broke the recall while trying to sort through things. It's all just a bit of a mess right now. I tried the attempt you laid out there, but it wasn't working for me. See the demo I linked to above: you can put a break in success and do xhr.responseText to see it. I'll try it again, though: maybe I was just on crack. Thanks for helping me hash this out. I really appreciate it!
- September 29, 2009, Bill wrote: Hrm. Well I don't get the xhr in success; but in complete as I illustrated in my 7:35am post today. In success I get the nicely evaluated JSON object data which I store in a class level variable called "returnData" so that I have access to it in complete (because in complete you get the raw JSON string and not the evaluated object out of xhr). I'll put my updated demo out on oak.sbcs.com - granted, this won't have the recall stuff - but it should show you how it's working for me. My demo polls "source.txt" instead of source.json becuase that server won't serve file extensions that aren't registered with it. http://oak.sbcs.com/demo/jqpost.html Bill
- September 29, 2009, JQuery PeriodicalUpdater Updated Again | Enfranchised Mind wrote: [...] to some excellent conversation over here, the JQuery PeriodicalUpdater is back in play, and now with change [...]
- September 29, 2009, Robert Fischer wrote: Thanks to this conversation, I got JQuery PeriodicalUpdater up and running again: http://demo.smokejumperit.com/demo.html Is there a good way to determine equality between JSON objects? (w/o add'l jQuery plugins)
- September 29, 2009, Bill wrote: It almost works :O( On line 71 I get the following error in firefox: xhr.responseText.trim is not a function var rawData = xhr.responseText.trim();\r\n It seems string.trim() isn't implemented for some reason. I get the same error in chrome as well.
- September 29, 2009, Robert Fischer wrote: Odd: how is it working for me?
- September 29, 2009, Robert Fischer wrote: Fixed.
- September 29, 2009, Bill wrote: Excellent; now it's working for me too. Good job!
- September 30, 2009, Robert Fischer wrote: I wish GitHub reflected branches better. Branches don't really fit into the conception that they're presenting with their GUI.

This article was a post on the EnfranchisedMind blog. EnfranchisedMind Blog by Robert Fischer, Brian Hurt, and Other Authors is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
