gpt4 book ai didi

javascript - xmlhttp.status 在本地主机 XAMPP 中返回 0

转载 作者:行者123 更新时间:2023-12-03 06:33:50 25 4
gpt4 key购买 nike

我是一名初学者,正在为我的学校作业开发一个应用程序。目前,我正在使用 XAMPP 来托管我的应用程序,并使用 phpmyadmin 来托管我的数据库。我面临的问题是 xmlhttp.status 返回 0 而不是 200。我之前使用 microsoft azure 而不是 XAMPP 的项目没有这个问题。

这是我在index.html中的脚本代码

var userid;
var password;
function serverURL(){
return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
}
function login(){
userid = $("#userid").val();
password = $("#password").val();
if (validate()){
alert("validate pass");
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "/login.php";
url += "?userid=" + userid + "&password=" + password;
alert(url);
xmlhttp.onreadystatechange=function() {
alert("xmlhttponready running");
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.status);
alert(xmlhttp.readyState);

}
getLoginResult(xmlhttp.responseText);
}


xmlhttp.open("GET", url, true);
xmlhttp.send();
}
}

这是我的login.php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
$conn = new mysqli("127.0.0.1", "root", "root", "bencoolen");
$userid = $_GET["userid"];
$password = $_GET['password'];
$query = "SELECT count(*) as found from profiles where userid ='" .
$userid . "' and password = '" . $password . "'";
$result = $conn->query($query);
$count = $result->fetch_array(MYSQLI_NUM);
$json_out = "[" . json_encode(array("result"=>$count[0])) . "]";
echo $json_out;
$conn->close();
}
catch(Exception $e) {
$json_out = "[".json_encode(array("result"=>0))."]";
echo $json_out;

}

PHP 返回结果='0'我个人觉得问题可能出在

function serverURL(){
return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
}

请指教:(

最佳答案

一些小错误:

您的 saveUrl 函数返回“http://localhost/webP/FYP/workshop/platforms/android/assets/www/

现在当你尝试连接时

var url = serverURL() + "/login.php";

网址变为 “http://localhost/webP/FYP/workshop/platforms/android/assets/www//login.php

我预计您需要 json 响应。那你为什么要添加呢? "[".json_encode()."]"

json_encode(array("result"=>0))

输出:

{"result":0}

getLoginResult(xmlhttp.responseText);

您正在远离 if block 。这是您收到 0 回复的主要原因。为此,让我解释一下 xmlhttp

当ajax请求被触发时,完成有4种状态

state value = 0 :=> (state = UNSET): the xmlhttp instance is initiated 
state value = 1 :=> (state = OPENED): The browser sends the data to the server
state value = 2 :=> (state = HEADERS_RECEIVED): request reached at server
state value = 3 :=> (state = LOADING): server processing the request
state value = 4 :=> (state = DONE): response reached from server to browser

现在进入编码部分:

    // When ever there is a state chgange in
// the xmlhttp object
xmlhttp.onreadystatechange=function() {
alert(xmlhttp.readyState);

// when the response from server is reached (state = 4)
// and the response header http code is 200 (xmlhttp.status == 200)
// do the following
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.status);
alert(xmlhttp.readyState);
getLoginResult(xmlhttp.responseText);
}

}

但是正如您保留的那样 getLoginResult(xmlhttp.responseText);在 if block 之外,因此当状态为零时(意味着请求未发送到服务器时)执​​行

试试这个js代码:

var userid;
var password;
function serverURL() {
return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
}

function login(){
userid = $("#userid").val();
password = $("#password").val();
if (validate()){
alert("validate pass");
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "login.php";
url += "?userid=" + userid + "&password=" + password;
alert(url);
xmlhttp.onreadystatechange=function() {
alert("xmlhttponready running");
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.status);
alert(xmlhttp.readyState);
getLoginResult(xmlhttp.responseText);
}

}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
}

你的 php 代码:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
$conn = new mysqli("127.0.0.1", "root", "root", "bencoolen");
$userid = $_GET["userid"];
$password = $_GET['password'];
$query = "SELECT count(*) as found from profiles where userid ='" .
$userid . "' and password = '" . $password . "'";
$result = $conn->query($query);
$count = $result->fetch_array(MYSQLI_NUM);
$json_out = json_encode(array("result"=>$count[0]));
echo $json_out;
$conn->close();
}
catch(Exception $e) {
$json_out = json_encode(array("result"=>0));
echo $json_out;

}
?>

建议

当您使用 jquery 时,请尝试使用 jquery.ajax() 代替 XMLHttpRequest (如果您是出于学习目的这样做,那么就可以了)。这将解决您的浏览器兼容性问题。

为了避免sql注入(inject),请尝试使用mysqli::prepare。 http://php.net/manual/en/mysqli.prepare.php

有关 XMLHTTPRequest 的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

关于javascript - xmlhttp.status 在本地主机 XAMPP 中返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com