Current location: Hot Scripts Forums » General Web Coding » JavaScript » AJAX - Using the POST Method instead of GET


AJAX - Using the POST Method instead of GET

Reply
  #1 (permalink)  
Old 02-20-07, 09:28 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
AJAX - Using the POST Method instead of GET

I have became more interested in Ajax and from the use of it, I learned that it can make the process sooo much faster than before in my web applications. I first started using this script to get me started

Code:
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="selectusers.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("totalusersDIV").innerHTML=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
I would like to start using the POST method instead of GET because of the limitations of the query string. It would be much appreciated if someone could suggest on how change the coding so its using the POST method.
Reply With Quote
  #2 (permalink)  
Old 02-21-07, 01:57 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
change this
Code:
xmlHttp.open("GET",url,true)
into
Code:
xmlHttp.open("POST",url,true)
Add this:
Code:
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
(add it after xmlHttp.open();

Then you don't send "null", but a urlencoded string like this:
Code:
xmlHttp.send("field1=value1&field2=value2");
That should do the trick.
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
  #3 (permalink)  
Old 02-21-07, 11:38 AM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
It doesnt work

Index.php
Code:
<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>

<form> 
Select a User:
<select name="users" onChange="showUser(this.value)">
<option value="Peter">Peter Griffin</option>
<option value="Lois">Lois Griffin</option>
<option value="Glenn">Glenn Quagmire</option>
<option value="Joseph">Joseph Swanson</option>
</select>
</form>

<p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p>

</body>
</html>
selectuser.js
Code:
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="getuser.php"

url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send("users="+str.users.value)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
Reply With Quote
  #4 (permalink)  
Old 02-21-07, 11:40 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
Make sure you send the correct post body in your .send().

I think "str.users.value" is not correct. Try alerting it.
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
  #5 (permalink)  
Old 02-21-07, 12:18 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
I got it work.....

Code:
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="grabdata.php"
var params = "users=Lois"

xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-Length", params.length);

xmlHttp.send(params)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
I am still concerned. I removed this code from there

Code:
url=url+"&sid="+Math.random()
I was told that with this code it prevents it from being cached. Is this needed since Im using the POST method?

and another thing Im having trouble with is this. You'll notice this..

Code:
var params = "users=Lois"
Yes you are correct. Using this str.users.value is not correct. How can I fix this?

Last edited by mcrob; 02-21-07 at 12:23 PM.
Reply With Quote
  #6 (permalink)  
Old 02-21-07, 01:53 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You will need the random number there to ensure the "answer" isn't cached, regardless of method used. It simply fools the browser into thinking that you're requesting a new page by modifying the querystring in the url. Obviously, the server should ignore this number.

"str.users.value" will be treated as a string since it's got quotes.
In this case, str is a string so you'll just use the variable as it is:

Code:
var params = "users="+str;
Btw, forgot to mention that the ampersand should have been a questionmark in the querystring.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 02-21-07 at 02:02 PM.
Reply With Quote
  #7 (permalink)  
Old 02-21-07, 03:13 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>

<form onSubmit="showUsers(this); return false"> 
  <p>Select a User:
    <select name="users">
      <option value="Peter">Peter Griffin</option>
      <option value="Lois">Lois Griffin</option>
      <option value="Glenn">Glenn Quagmire</option>
      <option value="Joseph">Joseph Swanson</option>
    </select>
</p>
  <p>
    <input name="username" type="text" id="username">
  </p>
  <p>
    <input type="submit" id="submit_button" name="Submit" value="Submit">
</p>
</form>

<p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p>

</body>
</html>
Everything is good but now when I press Submit it keeps showing..
[object HTMLFormElement]

I have encountered this problem before but I do not remember how to fix this issue..
Reply With Quote
  #8 (permalink)  
Old 02-21-07, 03:46 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Going with the above post, I checked the javascript console and the reason why its displaying [object HTMLFormElement] is because....

The Element referenced by ID/NAME in the global scope.

How can I fix this?
Reply With Quote
  #9 (permalink)  
Old 02-21-07, 04:06 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Never mind I fixed it. This is what i did...

error in this code
Code:
var params = "username="+usernamedata+ "&password="+passworddata
This was the correct code. I had to include the .value
Code:
var params = "username="+usernamedata.value+ "&password="+passworddata.value
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT:: how to solve post method error on asp page in Linux & Apache Server? j14nhAo ASP 2 02-24-06 12:33 PM
need Java script ( Ajax) Calendar, tree, grid curtisannev Job Offers & Assistance 1 10-30-05 08:59 PM
method post values kept nicpon PHP 2 09-29-05 06:18 PM
method = post By php ???????? a-Genius PHP 5 02-03-04 01:33 AM
one main method sniperx Everything Java 6 01-23-04 06:02 AM


All times are GMT -5. The time now is 11:25 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.