-
Javascript: get remote data in one method call
Posted on October 8th, 2009 No commentsI came across this useful bit of javascript when I had some parameters and wanted to call the server and get some data from the db(preferably in a nice json object). All in one line, as if I was calling a local function without having to mess with callbacks. ajax and jQuery make this very simple.
Here’s what the code looks like:
var jsonLoader = function(url, params) { this.url = url; this.params = params; this.retrievedData = {}; this.getRawData(); }; jsonLoader.prototype.getRawData = function() { var json = $.ajax({ type: 'POST', contentType: "application/json; charset=utf-8", url: this.url, data: JSON.stringify(this.params), dataType: 'json', success: this.setRawData(this), async: false }); }; jsonLoader.prototype.setRawData = function(self) { return function(json) { self.retrievedData = json; }; };And you’d call it like this:
var params = new Object(); params.var1 = "something"; params.var2 = 22; var data = new jsonLoader("/WebService/MyMethod", params); data.retrievedData.d.{json object name returned by the server}The method blocks until data is retrieved(async: false), so you can start using data right away.
Make sure you have jQuery and JSON libraries loaded.
Credit to the original jsonLoader sniplet author for the idea, shown here tweaked to accept parameters.
-
All WebKits are not created eq…
Posted on October 8th, 2009 No commentsAll WebKits are not created equal: http://bit.ly/3Y5iNT (WebKit powers Safari, Chrome, iPhone, Pre, Android to name a few.)
-
Brilliant bit of marketing fro…
Posted on October 8th, 2009 No commentsBrilliant bit of marketing from the North Face. New products: sticks, rocks, and leaves! http://bit.ly/q66hq


Recent Comments