我有一个javascript函数,它根据生成在名为 ajax.js
的文件中使用的浏览器创建XMLHttpRequest对象或ActiveObjectHTML: user.php
<ul id="list-of-developers">
<li class="list-title"><strong data-new-link="true">DEVELOPERS</strong>
</li>
</ul>
JAVASCRIPT : ajax.js
function ajaxObj2(meth, url, contype){
var xhttp;
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open(meth, url, true);
xhttp.setRequestHeader("Content-type",contype);
xhttp.setRequestHeader("Access-Control-Allow-Origin", "http://localhost:9000/");
return xhttp;
}
function ajaxReturn(x){
if(x.readyState==4 && x.status ==200)
{
return true;
}
}
在我的user.php文件中,其中包含我的html和脚本标记,包括文档头部的 ajax.js 文件。
以下是javascript与PHP连接的方式(注意:此标记位于body标记之外的文档底部)
JAVASCRIPT:user.php(内联)
<script>
window.onload = function()
{
var u = "<?= $u ?>";
var r = "<?= $user_role ?>";
var x = ajaxObj2('GET', 'retrieveDevelopers.php','text/plain');
x.onreadystatechange = function()
{
alert("H u is: "+u+ " r is: "+r + " RESPONSE: " + x.responseText.trim());
if(ajaxReturn(x) ==true)
{
alert("got here");
var list_of_devs = document.getElementById("list-of-developers");
console.log(list_of_devs);
list_of_devs.innerHTML = x.responseText.trim();
}
}
console.log(u);
x.send("u="+u + "&r="+r);
}
</script>
PHP:在retrieveDevelopers.php
<?php
$x = $_GET["u"];
$m = $_GET["r"];
if(isset($_GET["u"]))
{
include_once("phpincludes/db_connx.php");
//echo
$person= preg_replace('#[^a-z0-9]#i','',$_GET["u"]);
$person_sql = "SELECT * FROM USERS WHERE USER_ID=(?) AND ACTIVATED=(?)";
$person_params = array($person, 1);
$person_options = array("Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED);
$person_result = sqlsrv_query($db_connx, $person_sql,$person_params, $person_options);
$person_state = sqlsrv_num_rows($person_result);
if($person_state < 1)
{
echo "Failed to verify user";
exit();
}
else
{
echo("It Worked");
exit();
}
}
echo "failed";
exit();
?>
我收到错误&#34;注意:未定义的索引:u在第9行&#34;和&#34;注意:未定义的索引:r在10&#34;
我不明白为什么会这样。数据库连接没有问题。 除数据库连接脚本
外,这些文件都在同一文件夹中文件系统:
系统信息
Apache Version:2.4.17
PHP Version: 5.6.15
MICROSOFT SQL SERVER 2012
Localhost: localhost:9000
答案 0 :(得分:0)
你没有发送var u和r尝试类似这样的东西
xhttp.open("GET", "retrieveDevelopers.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("u=value&u=value");
或
xhttp.open("GET", "retrieveDevelopers.php?u=value&r=value", true);
xhttp.send();