<?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>Webburners &#187; ajax using get and post method</title>
	<atom:link href="http://www.webburners.com/tag/ajax-using-get-and-post-method/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webburners.com</link>
	<description>We Burn the web with our expertise</description>
	<lastBuildDate>Thu, 08 Apr 2010 12:53:42 +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>How to use post and get method in ajax</title>
		<link>http://www.webburners.com/2009/04/how-to-use-post-and-get-method-in-ajax/</link>
		<comments>http://www.webburners.com/2009/04/how-to-use-post-and-get-method-in-ajax/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 14:57:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[ajax post method]]></category>
		<category><![CDATA[ajax using get and post method]]></category>

		<guid isPermaLink="false">http://www.webburners.com/?p=289</guid>
		<description><![CDATA[Using GET method
Now we open a connection using the GET method.

var url = "get_data.php";
var params = "lorem=ipsum&#38;name=binny";
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 &#38;&#38; http.status == 200) {
		alert(http.responseText);
	}
}
http.send(null);

I really hope that this much is clear for you &#8211; I am assuming that you know a bit of Ajax [...]]]></description>
			<content:encoded><![CDATA[<h3>Using GET method</h3>
<p>Now we open a connection using the GET method.</p>
<pre><code class="javascript">
var url = "get_data.php";
var params = "lorem=ipsum&amp;name=binny";
http.open("<span class="special">GET</span>", url<span class="special">+"?"+params</span>, true);
http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 &amp;&amp; http.status == 200) {
		alert(http.responseText);
	}
}
http.send(<span class="special">null</span>);
</code></pre>
<p>I really hope that this much is clear for you &#8211; I am assuming that you know a bit of Ajax coding. If you don&#8217;t, please read a <a title="A Gentle Introduction to Ajax" href="http://www.openjs.com/ajax/tutorial/">ajax tutorial</a> that explains these parts before continuing.</p>
<h3>POST method</h3>
<p>We are going to make some modifications so POST method will be used when sending the request&#8230;</p>
<pre><code class="javascript">
var url = "get_data.php";
var params = "lorem=ipsum&amp;name=binny";
http.open("<span class="highlight">POST</span>", url, true);

<span class="highlight">//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");</span>

http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 &amp;&amp; http.status == 200) {
		alert(http.responseText);
	}
}
http.send(<span class="highlight">params</span>);
</code></pre>
<p>The first change(and the most obvious one) is that I changed the first argument of the <code>open</code> function from GET to POST. Also notice the difference in the second argument &#8211; in the GET method, we send the parameters along with the url separated by a &#8216;?&#8217; character&#8230;</p>
<pre><code class="javascript">http.open("GET",<span class="special">url+"?"+params</span>, true);</code></pre>
<p>But in the POST method we will use just the url as the second argument. We will send the parameters later.</p>
<pre><code class="javascript">http.open("POST", <span class="special">url</span>, true);</code></pre>
<p>Some <a title="HTTP/1.1: Header Field Definitions" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">http headers</a> must be set along with any POST request. So we set them in these lines&#8230;</p>
<pre><code class="javascript">http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");</code></pre>
<p>With the above lines we are basically saying that the data send is in the format of a form submission. We also give the length of the parameters we are sending.</p>
<pre><code class="javascript">http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 &amp;&amp; http.status == 200) {
		alert(http.responseText);
	}
}</code></pre>
<p>We set a handler for the &#8216;ready state&#8217; change event. This is the same handler we used for the GET method. You can use the <code>http.responseText</code> here &#8211; insert into a div using innerHTML(<a class="mypoges blog" title="AHAH(Asynchronous HTML over HTTP) - AJAX 2.0" href="http://binnyva.blogspot.com/2005/11/ahahasynchronous-html-over-http-ajax.html">AHAH</a>), eval it(<a class="blog mypages" title="Ajax Response Data Formats" href="http://binnyva.blogspot.com/2006/03/ajax-response-data-formats.html">JSON</a>) or anything else.</p>
<pre><code class="javascript">http.send(<span class="highlight">params</span>);</code></pre>
<p>Finally, we send the parameters with the request. The given url is loaded only after this line is called. In the GET method, the parameter will be a null value. But in the POST method, the data to be send will be send as the argument of the <code>send</code> function. The <code>params</code> variable was declared in the second line as &#8220;lorem=ipsum&amp;name=binny&#8221; &#8211; so we send two parameters &#8211; &#8216;lorem&#8217; and &#8216;name&#8217; with the values &#8216;ipsum&#8217; and &#8216;binny&#8217; respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webburners.com/2009/04/how-to-use-post-and-get-method-in-ajax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
