简体   繁体   中英

AJAX/PHP/JS readyState = 1 and Status = 0 ALWAYS. No response text

I've been trying lately to use this sample of AJAX to compare form data to an SQL database from one http://www.example.com domain. My issue is that the readyState is always 1 and my Status is always 0. It is expecting 4 and 200 respectively. It also always returns responseText="" I've looked all over StackOverflow but have unsuccessfully found anything helpful.

I've boggled my mind over what could be the issue, but I just can't seem to get it to work.

*I've also tried to set file permissions on both the JS and PHP, but it functions the same.

*I'm using a dedicated web server to host all this, and I have no problem running most scripts.

//HTML GenerateRep.html

Excuse the lack of < and > tags missing, the code won't appear without them.

form id="formgen" onsubmit="GenRep(this)"
....form stuff....
button id="submit" type="submit">Submit</button

//JAVASCRIPT GenerateRep.js

function GenRep(formgen) {

var email = formgen['repemail'];
var hash = formgen['reppass'];
var first = formgen['firstname'];
var last = formgen['lastname'];
var territory = formgen['territory'];

hash.value = CryptoJS.SHA256(hash.value);

var login = email + ";" + hash.value + ";" + first + ";" + last + ";" + territory;

Login(login);


}

function Login(login) {
    var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                if(xhttp.responseText == "VALID") {
                    window.location.href = "success.html";
                } else if (xhttp.responseText == "INVALID") {
                    $("#login_error").text("Failed! Plese check your info.");
                } else {
                    window.location.href = "error.php";
                }
            }
};
xhttp.open("GET", "Validate.php?q=" + login, true);
xhttp.send();
}

//PHP Validate.php

<?php
header('Access-Control-Allow-Origin: *');
include ("ConnectDB.php");

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

} 

//THIS IS A TEST TO SEE IF reponseText FUNCTIONS. IT DOES NOT. 
//echo "testecho";


$whole = $_REQUEST['q'];

$userPass = explode (";", $whole);


$sql1 = "SELECT UName FROM Reps WHERE UName = '$userPass[0]'";
$result = $conn->query($sql1);

if ($result->num_rows > 0) {
$conn->close();
echo "INVALID";

} else {

$sql = "INSERT INTO Reps (UName, Pass, FName, LName, Territory) VALUES ('$userPass[0]', '$userPass[1]', '$userPass[2]', '$userPass[3]', $userPass[4])";

if ($conn->query($sql) === FALSE) {
$conn->close();
echo "Error: " . $sql . "<br>" . $conn->error;

}


$conn->close();
echo "VALID";

}
?>

I previously "commented" instead of creating an "Answer" because I wasn't suggesting a fix, just a debug step to make sure what you thought was happening, was actually happening.

Since my suggestion helped you figure out the problem, I created this "Answer" in case you want to give me "credit". :-)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM