-
-
Save masqita/247976 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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