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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    XMLHttpRequest POST not sending

    When I send via GET using XMLHttpRequest to a PHP script then the data is received, but if I do the same with POST then nothing makes it through.

    Here's my simple JavaScript function (tested in Firefox only) which works with GET but apparently won't send POST data:
    Code:
    function requestXML(url, post) {
    	xml_request = false;
    	
        if (window.XMLHttpRequest) {
        	try {
    			xml_request = new XMLHttpRequest();
            } catch(e) {
    			xml_request = false;
            }
        }
    	
    	if (xml_request) {
    		xml_request.onreadystatechange = processChange;
    		
    		// Check for POST values.
    		if (post) {
    			xml_request.open("POST", url, true);
    			xml_request.send(post);
    		} else {
    			xml_request.open("GET", url, true);
    			xml_request.send("");
    		}
    	}
    }
    It is called like-so with POST:
    Code:
    onClick="requestXML('http://localhost/update.php', this.name + '=' + this.value)"
    It is called like-so with GET:
    Code:
    onClick="requestXML('http://localhost/update.php?' + this.name + '=' + this.value)"
    The PHP script that responds simply executes print_r($_REQUEST); and the JavaScript function processChange simply displays the output (or an error) in an alert box.

    If I send via GET, then the alert box displays the values sent. With POST, the alert box is empty.

    Any ideas what might be gumming up the works?
    Last edited by bauhsoj; 08-21-2005 at 02:11 AM.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You run any debugging code on the php that you are calling to seeif you are retrievein the passed value correctly. Are you sure that It is going into the post branch of the if statement?

    Also your code will not work in IE.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    here's what i use. it processes post and get variables as expected.
    Code:
    var x;
    function loadtext(a){
    if (window.XMLHttpRequest)  {
    x=new XMLHttpRequest();
    x.onreadystatechange=loadx;
    x.open("GET",a,true); x.send(null);
    } else if (window.ActiveXObject)  {
    x=new ActiveXObject("Microsoft.XMLHTTP");
    if (x) {x.onreadystatechange=loadx;
    x.open("GET",a,true); x.send();}}}
    
    function loadx(){ if (x.readyState==4) if (x.status==200) alert(x.responseText);}
    call from whatever with loadtext('filename.ext');

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51
    You run any debugging code on the php that you are calling to seeif you are retrievein the passed value correctly. Are you sure that It is going into the post branch of the if statement?
    The only code that is being run on the PHP side is the following:
    PHP Code:
    print("=====Results=====\n");
    print_r($_REQUEST); 
    If a form is submitted via POST or GET then $_REQUEST will hold both of those. I see the "Results" header in the alert box and then the empty array of $_REQUEST. Same as when I do so with $_POST specifically. If I send a GET request then the data I sent will show up below "Results".

    Just to make sure the POST branch was being reached I commented out the GET branch and the rest of the conditional. I got the same results.

    Quote Originally Posted by A1ien51
    Also your code will not work in IE.
    I plan to add the IE compatible ActiveX code later.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need proper MIME type for post data:
    Code:
    xml_request.setRequestHeader('Content-Type',
        'application/x-www-form-urlencoded');
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And it should come between "open" and "send"
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #7
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, I will try adding the mime type. Thanks!

  • #8
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I get the same behavior as this...

    Is there anyone out there that can help me to resolve this:

    ajaxRequest.open('POST', URL, true);
    ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajaxRequest.send(null);

    ...does not work for me. $_GET fills but $_POST fails to. (On all browsers)

  • #9
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    xhr.open('POST', url, true);
    xhr.onreadystatechange = stateChange;
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", parameters.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.send(parameters);
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #10
    New to the CF scene
    Join Date
    Jul 2010
    Location
    New Hampshire
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by Vladdy View Post
    You need proper MIME type for post data:
    Code:
    xml_request.setRequestHeader('Content-Type',
        'application/x-www-form-urlencoded');
    I'd forgotten this step... thanks very much for posting this, way back when!


  •  

    Posting Permissions

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