I'm developing a small gadget for our intranet and I have a small php file on our server just for first test. Now, everything is working if I'm using GET request but I would like to use POST to not have all data in URL. Unfortunately I'm not possible to get any data by POST request.
Working solution with GET:
function ajax_getData() {
var error = false;
ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open("GET", "http://server.com/index.php" + "?update&login=" + g_LOGIN + "&pass=" + g_PASS, true);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
if (ajax.status === 200) {
try {
$('#div0').html(ajax.responseText);
// do something
} catch (e) {
error = true;
}
} else {
error = true;
}
} else {
}
if (error) {
// handle error
}
};
ajax.send(null);
}
index.php
echo(var_dump($_POST));
Now, my not working POST solution:
function ajax_getData() {
var error = false;
ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open("POST", "http://server.com/index.php", false);
//ajax.open("POST", "http://server.com/index.php", true);
ajax.send("update&login=" + g_LOGIN + "&pass=" + g_PASS);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
if (ajax.status === 200) {
try {
$('#div0').html(ajax.responseText);
// do something
} catch (e) {
error = true;
}
} else {
error = true;
}
} else {
}
if (error) {
// handle error
}
};
ajax.send(null);
}
In case POST solution I always get from index.php only 'Array(0)'.
What about using new XMLHttpRequest() instead of new ActiveXObject("Msxml2.XMLHTTP");
From http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp :
All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
variable=new XMLHttpRequest();
Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object:
variable=new ActiveXObject("Microsoft.XMLHTTP");