...

View Full Version : XMLHttpRequest POST not sending



bauhsoj
08-21-2005, 01:07 AM
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:
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:
onClick="requestXML('http://localhost/update.php', this.name + '=' + this.value)"

It is called like-so with GET:
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?

A1ien51
08-21-2005, 05:06 AM
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

subhailc
08-21-2005, 08:16 AM
here's what i use. it processes post and get variables as expected.

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');

bauhsoj
08-21-2005, 05:57 PM
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:
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.


Also your code will not work in IE.

I plan to add the IE compatible ActiveX code later.

Vladdy
08-22-2005, 05:55 AM
You need proper MIME type for post data:

xml_request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

brothercake
08-23-2005, 04:00 PM
And it should come between "open" and "send"

bauhsoj
08-24-2005, 03:53 PM
Okay, I will try adding the mime type. Thanks! :)

cjohnsoia
08-13-2008, 10:16 PM
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)

A1ien51
08-14-2008, 02:48 AM
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

dldnh
07-01-2010, 09:12 AM
You need proper MIME type for post data:

xml_request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

I'd forgotten this step... thanks very much for posting this, way back when!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum