I'm trying to do a XMLHttpRequest for a login via POST and have actually done this before. I tried to recycle my code from a working project and changed it slightly but it's not working anymore and I honestly have no clue why.
The sent POST is always empty.
var user = document.getElementById("username").value;
var pw = document.getElementById("password").value;
if(user != "" && pw != "") {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status === 200) {
var response = this.responseText;
console.log(response);
if(response == "-1") {
//do stuff
} else if(response == "1") {
window.location.href = "/dashboard/"
}
}
};
var params = "username="+user+"&password="+pw+"&do=login";
xmlhttp.open("POST","provider/SessionServices.php",true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(params);
It actually does the request and I receive my error code for missing POST params. I also debugged using print_r($_POST) and had to see that there is never POST data sent.
EDIT: SessionServices.php
print_r($_POST);
if(isset($_POST["username"]) && !empty($_POST["username"]) &&
isset($_POST["password"]) && !empty($_POST["password"])) {
if ($_POST["do"] == "login") {
//login request by xmlhhtp
$user = $_POST["username"];
$pw = $_POST["password"];
//do stuff
} else {
echo "XH-102"; //unknown action
}
} else {
echo "XH-101"; //user,pw,do empty or not set
it always outputs "Array ( ) XH-101"
Okay, seems that I have found the issue. XMLHttpRequests (or just using POST?) seemingly not work locally or atleast using XAMPP. When I uploaded it to my webserver it just worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.