Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2008, 02:05 PM   PM User | #1
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
AJAX POST Problem

I wrote up a very simple function to do AJAX POSTs and GETs. GETs work, but POSTs don't, for some reason. The function is this:

Code:
AJAX=function(options){
	var request;
	try{
		// Opera 8.0+, Firefox, Safari
		request = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser doesn't support AJAX.");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
  request.onreadystatechange = function(){
  	if(request.readyState===4){
  		options.callback(request.responseText);
  	}
  }
  if(options.method.toUpperCase()==="POST"){
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }
  alert("Open: "+((options.params && options.method.toUpperCase()==='GET')?(options.url+"?"+options.params):options.url)+", Send: "+((options.params && options.method.toUpperCase()==="POST")?options.params:null));
	request.open(options.method.toUpperCase(), (options.params && options.method.toUpperCase()==='GET')?(options.url+"?"+options.params):options.url, true);
  request.send((options.params && options.method.toUpperCase()==="POST")?options.params:null);
}
When I tried to do the POST request without setting the content-type header, nothing happened. But when I set the content-type header, it behaves like a form (I call it from the page edit.php?page=asdf, and when I try the post request it goes to edit.php).

I'm calling the request like this:
Code:
AJAX({
    url:'save.php',
    method:'POST',
    callback:function(a){
      alert(a);
    },
    params:params
 });
I'm calling it when a form gets submitted, but i have it return false so the form won't submit.

Any ideas appreciated!

Last edited by binaryWeapon; 11-29-2008 at 02:09 PM..
binaryWeapon is offline   Reply With Quote
Old 11-28-2008, 02:21 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
perhaps you should also set the Content-Length and Connection headers for the POST
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 11-29-2008, 01:55 AM   PM User | #3
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
Thanks for the reply.

When putting in those two headers, I noticed that I was sending request headers before I actually opened the connection. I fixed this, and that fixed the odd redirect. Everything works now! =)

Thanks for mentioning the other 2 headers, I forgot about them and wouldn't have noticed my error if I hadn't went back to put them in!
binaryWeapon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:26 AM.


Advertisement
Log in to turn off these ads.