<?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>Stephen Gray&#039;s dev blog &#187; scope</title>
	<atom:link href="http://blog.s-gray.com/tag/scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.s-gray.com</link>
	<description>e-commerce and integrations</description>
	<lastBuildDate>Sun, 17 Jan 2010 09:32:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery retrieving the data from an AJAX call into the global scope</title>
		<link>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/</link>
		<comments>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 09:06:16 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=133</guid>
		<description><![CDATA[You make an AJAX call within a jQuery function and you want the call to be synchronous so that you can use the data from the call in the function scope. This tutorial will show you how.]]></description>
			<content:encoded><![CDATA[<p>Another quick one. Reading around it seems to be an issue. You make an AJAX call within a function and you want the call to be synchronous so that you can use the data from the call in the function scope.</p>
<p>AJAX calls are obviously asynchronous. jQuery has a &#8216;async: false&#8217; option in AJAX calls but it is ignored by most (maybe all?) browsers. Using this method you can force a synchronous request and retrieve that data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p133code1'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1331"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p133code1"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> myVariable <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'someScript.php'</span><span style="color: #339933;">,</span>
        async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myVariable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So .responseText retrieves the text value of the response from the AJAX call. And because we&#8217;re assigning to a variable we are forcing a synchronous request. You can now use that variable as you see fit.</p>
<p>This causes an issue when you are trying to retrieve a JSON object from an AJAX call into the function scope. The best way I&#8217;ve found so far is to retrieve the JSON as a string and use a jquery JSON plugin to convert it into an accessible object. Or you could just use the &#8216;eval&#8217; method.</p>
<p>Stephen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

