I have an Ajax call written in prototype and need to convert it to jquery. I’m not well versed in requestHeaders and the like, so I’m a bit stumped. Here’s the prototype version:
function poll_for_update(feed_id, last_modified, link) {
setTimeout(function() {
new Ajax.Request('/feeds/' + feed_id, {
method: 'get',
requestHeaders: { 'If-Modified-Since': last_modified },
onComplete: function(transport) {
if (transport.status == 304) {
poll_for_update(feed_id, last_modified, link);
} else if (transport.status == 200) {
$('feed_' + feed_id).innerHTML = transport.responseText
} else {
link.innerHTML = 'error'
}
}
}) },
1000
)
}
Since I can extract the feed_id and last_modified dates from the link, my early version of the corresponding jquery functions looks like this:
function poll_for_update(link) {
var feed_id = $(link).attr('feed_id')
var last_modified = $(link).attr('last_modified')
setTimeout(function() {
$.ajax({
url: show_path(link),
type: 'get',
ifModified: true,
headers: { 'If-Modified-Since': last_modified },
statusCode: {
404: function() {
alert('404');
},
304: function() {
alert('304');
},
200: function() {
alert('200');
}
},
success: function( data ) {
alert('success');
},
complete: function() {
alert('complete');
}});
}, 1000)
}
A few things I can’t figure out:
- how do I extract
transport.statusfrom within the complete() function? - do I need the statusCode: clause, or is the complete: clause sufficient?
- is the ifModified clause necessary?
… etc.
(For the curious, this is a transcription of Adam Wiggins’s tutorial on how to build a queue-backed feed reader, which is super useful but somewhat outdated.)
TIA.
Here’s what I ended up with, and it appears to work. What wasn’t obvious to me was that the ‘transport’ object is passed as an argument to the completion() method. Once I realized that, the jquery implementation is quite close to the prototype version: