<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dec0der &#187; async</title>
	<atom:link href="http://blog.d27n.com/tags/async/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.d27n.com</link>
	<description>the d27n blog</description>
	<lastBuildDate>Fri, 27 Aug 2010 03:03:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript: get remote data in one method call</title>
		<link>http://blog.d27n.com/2009/10/08/javascript-get-remote-data-in-one-method-call/</link>
		<comments>http://blog.d27n.com/2009/10/08/javascript-get-remote-data-in-one-method-call/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 20:02:15 +0000</pubDate>
		<dc:creator>ilya</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://blog.d27n.com/?p=71</guid>
		<description><![CDATA[
			
		
I 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 [...]]]></description>
			<content:encoded><![CDATA[<div class="fblikebutton_button" style="float: right; margin-left: 10px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.d27n.com%2F2009%2F10%2F08%2Fjavascript-get-remote-data-in-one-method-call%2F&amp;layout=standard&amp;show-faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=dark" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:26px"></iframe>
		</div>
<p>I 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.</p>
<p>Here&#8217;s what the code looks like:</p>
<pre>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; }; };</pre>
<p>And you&#8217;d call it like this:</p>
<pre>  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}</pre>
<p>The method blocks until data is retrieved(async: false), so you can start using data right away.</p>
<p>Make sure you have <a href="http://jquery.com/">jQuery</a> and <a href="http://json.org/js.html">JSON</a> libraries loaded.</p>
<p>Credit to the original jsonLoader sniplet author for the idea, shown here tweaked to accept parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.d27n.com/2009/10/08/javascript-get-remote-data-in-one-method-call/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
