简体   繁体   English

AJAX / PHP / JS readyState = 1并且Status = 0总是。 无回复文字

[英]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. 我最近一直在尝试使用此AJAX示例将一个http://www.example.com域中的表单数据与SQL数据库进行比较。 My issue is that the readyState is always 1 and my Status is always 0. It is expecting 4 and 200 respectively. 我的问题是readyState始终为1,我的Status始终为0。期望分别为4和200。 It also always returns responseText="" I've looked all over StackOverflow but have unsuccessfully found anything helpful. 它也总是返回responseText =“”我在StackOverflow上都看过,但是没有找到任何有用的东西。

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. *我也尝试在JS和PHP上设置文件许可权,但功能相同。

*I'm using a dedicated web server to host all this, and I have no problem running most scripts. *我正在使用专用的Web服务器来托管所有这些,并且运行大多数脚本都没有问题。

//HTML GenerateRep.html // 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 // 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 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". 由于我的建议可以帮助您解决问题,因此我创建了这个“答案”,以防您想给我“信用”。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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