gpt4 book ai didi

javascript - 无法将数据从 PHP 传递到 AJAX

转载 作者:行者123 更新时间:2023-11-30 12:08:59 25 4
gpt4 key购买 nike

我有一个 html 格式的输入框。输入通过ajax搜索数据库,并在前端返回结果。问题是我没有从 PHP 获得结果。我不知道我做错了什么,所以我希望你们能从我这里得到更好的理解。

HTML

<body onload="AjaxFindPerson()">
.....
</body>

JS

    var xmlHttp = createXmlHttpRequestObject();



function AjaxFindPerson() {
if ((xmlHttp.readyState == 0 || xmlHttp.readyState == 4) && document.getElementById("PersonSearchInput").value != "") {
person = encodeURIComponent(document.getElementById("PersonSearchInput").value);
xmlHttp.open("GET", "../lib/search.php?email=" + person, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);

}
else {
document.getElementById('Label-Result').innerHTML = "";
document.getElementById('UserNameSearchResult').innerHTML = "";
$('#add-person-btn').attr("disabled", "disabled");
setTimeout('AjaxFindPerson()', 1000);
}
}


function handleServerResponse() {
if (xmlHttp.readyState == 4 ) {
if (xmlHttp.status == 200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
result = xmlDocumentElement.firstChild.data;

if (result[0] != false) {
document.getElementById('Label-Result').innerHTML = result[1];
document.getElementById('UserNameSearchResult').innerHTML = result[0];
$('#add-person-btn').removeAttr("disabled", "disabled");
}
else {
document.getElementById('Label-Result').innerHTML = result[1];
}

setTimeout('AjaxFindPerson()', 1000);
}
else {
alert('Somenthing went wrong when tried to get data from server'+ xmlHttp.readyState);
}
}
}

PHP

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
session_start();
define("DB_HOST", 'mysql6.000webhost.com');
define("DB_USER", '');
define("DB_PASSWORD", '');
define("DB_DATABSE", '');

echo '<response>';

$email = $_GET['email'];

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABSE, $conn);
$sq = mysql_query("SELECT UserEmail FROM Users");

$UserInfo = array();

while ($row = mysql_fetch_array($sq, MYSQL_ASSOC)) {
$UserInfo[] = $row['UserEmail'];
}


if (in_array($email, $UserInfo)) {
$result = mysql_query("SELECT UserName FROM Users WHERE UserEmail = '".$email."'");
$row = mysql_fetch_row($result);
$returnRes = array($row[0], "We found results"); //row[0] holds the UserN
echo $returnRes;
}
else {
$returnRes = array(false, "We couldn't find results");
echo $returnRes;
}

echo '</response>';


?>

如果我们单独检查 php-xml 文件,将会看到下面的图像: enter image description here

我是否需要通过其他方式将值传递给 xml-php?

PHP 更新 1我设法找到了一种正确返回数据的方法。这是更新“触摸”

 header('Content-Type: application/json');

if (in_array($email, $UserInfo)) {
$result = mysql_query("SELECT UserName FROM Users WHERE UserEmail = '".$email."'");
$row = mysql_fetch_row($result);
echo json_encode(array( 'found' => $row[0], 'msg' => "We found results"));
}
else {
echo json_encode(array( 'found' => null, 'msg' => "We couldn't find results"));
}

现在的问题是如何操作js文件来处理返回数组。我试了一下,但没有成功:

result = xmlDocumentElement.firstChild.data;

if (result['found'] != null) {
document.getElementById('Label-Result').innerHTML = result['msg'];
document.getElementById('UserNameSearchResult').innerHTML = result['found'];
$('#add-person-btn').removeAttr("disabled");
}
else {
document.getElementById('Label-Result').innerHTML = result['msg'];
}

**更新 2 个工作 JS **

我想出了如何从 PHP 检索数据。

xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
var result = JSON.parse(xmlDocumentElement.firstChild.data);

if (result['found'] != null) {
document.getElementById('Label-Result').innerHTML = result['msg'];
document.getElementById('UserNameSearchResult').innerHTML = result['found'];
$('#add-person-btn').removeAttr("disabled");
}
else {
document.getElementById('Label-Result').innerHTML = result['msg'];
}

现在所有代码都可以工作了!非常感谢你们!

为你们所有人+1!

最佳答案

四件事:

  1. send(null) 的用法似乎不对,只是不要在其中传递 null。
  2. 第二个是超时方法。您可以按照您使用它的方式在回调函数中调用它,或者在函数调用时使用名称代替字符串。
  3. 删除属性的用法也是错误的。它当前正在使用 set 方法,因为您提供了第二个参数。移除属性方法只需要一个属性名称。
  4. 我宁愿建议您为 application/json 设置一个 header ,并使用 json_encode() 方法返回数据。

关于javascript - 无法将数据从 PHP 传递到 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34382611/

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