Skip to content

Instantly share code, notes, and snippets.

@masqita
Forked from wycats/compare.js
Created December 3, 2009 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masqita/247976 to your computer and use it in GitHub Desktop.
Save masqita/247976 to your computer and use it in GitHub Desktop.
// prototype
new Ajax.Request("/your/mom", onSuccess: function(response) { $("lightbox_content").innerHTML = response.responseJSON.contents })
// jquery
$.getJSON("/your/mom", function(json) { $("#lightbox_content").html(json.contents) })
// prototype sprinkled with a bit of love (last command out of my head)
// Helpers to make JSON requests a bit cleaner.
Ajax.Json = {
get: function(uri, callback) {
new Ajax.Request(uri, {
method: 'get',
requestHeaders: {Accept: 'application/json'},
onSuccess: function(transport) { if (callback) callback(transport.responseJSON, transport) }
})
},
post: function(uri, obj, callback, error_callback, headers) {
new Ajax.Request(uri, {
method: 'post',
contentType: 'application/json',
postBody: Object.toJSON(obj),
requestHeaders: Object.extend({Accept: 'application/json'}, headers),
onComplete: function(transport) {
if (transport.request.success()) {
if (callback) callback(transport.responseJSON, transport);
} else {
if (error_callback) error_callback();
}
}
})
},
put: function(uri, obj, callback, error_callback) {
Ajax.Json.post(uri, obj, callback, error_callback, {'X-Http-Method-Override': 'PUT'})
},
del: function(uri, obj, callback, error_callback) {
Ajax.Json.post(uri, obj, callback, error_callback, {'X-Http-Method-Override': 'DELETE'})
}
}
# ... and every JSON request just becomes so much nicer
Ajax.Json.get("/your/mom", function(json) { $("lightbox_content").update(json.contents) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment