<?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>Code That Matters &#187; xmlHttpRequest</title>
	<atom:link href="http://www.codethatmatters.com/tag/xmlhttprequest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codethatmatters.com</link>
	<description>Pylons, rails, jQuery and other stuff that matters</description>
	<lastBuildDate>Sat, 16 Jul 2011 10:57:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Check for ajax requests in Pylons</title>
		<link>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/</link>
		<comments>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 15:00:11 +0000</pubDate>
		<dc:creator>jorgen</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[is_xhr]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[xmlHttpRequest]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=132</guid>
		<description><![CDATA[In todays world of web development Ajax is taken for granted and there is a ton of good JavaScript frameworks out there that makes Ajax development quite easy and enjoyable. Together with a nice MVC framework it is even better. Each framework has its own way of detecting Ajax calls or more correctly xmlHttpRequests, which [...]]]></description>
			<content:encoded><![CDATA[<p>In todays world of web development Ajax is taken for granted and there is a ton of good JavaScript frameworks out there that makes Ajax development quite easy and enjoyable. Together with a nice MVC framework it is even better. Each framework has its own way of detecting Ajax calls or more correctly xmlHttpRequests, which is a huge topic on its own.</p>
<p>For checking if a request is an Ajax request in Pylons you just use is_xhr in your controller, this is based on the fact that you are running your Pylons app as wsgi.</p>
<p>Make sure your importing the request class</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="kw1">from</span> pylons <span class="kw1">import</span> request</pre></div></div>

<p>Then in your controller function you can just do</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="co1"># Checks if request is a xmlHttpRequest (Ajax) and returns a def</span>
<span class="kw1">if</span> request.<span class="me1">is_xhr</span>:
&nbsp;
<span class="co1"># Code goes here</span>
&nbsp;
<span class="kw1">return</span> render_mako_def<span class="br0">&#40;</span><span class="st0">'template_path'</span>,<span class="st0">&quot;def_name&quot;</span><span class="br0">&#41;</span></pre></div></div>

<p>You can also return and render a full template if you need or think that works better with your function.</p>
<p>More information can be found at <a href="http://pylonshq.com/docs/en/0.9.7/advanced_pylons/paster/">PylonsHQ Paster &#8211; Documentation &#8211; WSGI, CLI scripts</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

