Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    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.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •