您好我是初学程序员,使用Javascript和PHP。我有一个按钮页面,我正在尝试ahref
按钮到各自的vendordetails.html页面。到达vendordetails.html后,php代码将运行到SELECT * where vendorid = $vendorid
并在html页面中显示*
我的情况
当我点击按钮#2时,发送的网址如下所示:
http://servername/..../vendordetails.html?vendorid=2
在^页面上,我可以收到' 2'使用getUrlVars()
函数从URL参数
(alert(decodeURIComponent(getUrlVars()["vendorid"])); // alerts the value '2'.
)
但是,当我拨打Ajax电话时,xmlhttp.status
会返回' 500' 而不是200,这意味着内部服务器出现错误,发送的参数可能会不对。我怀疑这是我的PHP代码,但根本找不到任何错误(它与工作显示按钮php文件类似),我想找一些指导!
vendordetails.html
var vendorid = decodeURIComponent(getUrlVars()["vendorid"]);
alert(decodeURIComponent(getUrlVars()["vendorid"])); //alerts 2
function displayvendordetails(){
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "/vendordetails.php";
url += "?vendorid=" + vendorid.trim();
//also tried using ?vendorid = decodeURIComponent(getUrlVars()["vendorid"])
alert(url); //alerts http://servername/..../vendordetails.php?vendorid=2
xmlhttp.onreadystatechange=function() {
//alert(xmlhttp.readyState); alerts 4
alert(xmlhttp.status); //alerts 500
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("hello working"); //not alerted, code stops working after if statement
getdisplayResult(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function getdisplayResult(response){
var arr = JSON.parse(response);
var i;
for(i = 0; i < arr.length; i++) {
if (arr[0].vendorname.trim() != ""){
vendorname = arr[0].vendorname;
//var vendorname; declared outside
$("#vendorname").html(arr[0].vendorname);
}
if (arr[0].vendortype.trim() != ""){
vendortype = arr[0].vendortype;
//var vendortype; declared outside
$("#vendortype").html(arr[0].vendortype);
}
if (arr[0].rebatepercentage != ""){
$("#rebatepercentage").html(arr[0].rebatepercentage + "%");
}
}
}
displayvendordetails();
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
vendordetails.php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "my_user", "my_password", "world");
$vendorid = $_GET['vendorid'];
$result = $conn->query("SELECT vendorname, vendortype, rebatepercentage WHERE vendorid = '" . $vendorid. "' ");
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"vendorname":' . $rs["vendorname"] . ',';
$outp .= '"vendortype":"' . $rs["vendortype"] . '",';
$outp .= '"rebatepercentage":"' . $rs["rebatepercentage"] .
'"}';
}
$outp .="]";
$conn->close();
echo($outp);
该PHP代码应返回:
[{"vendorname":"Test","vendortype":"Apparel","rebatepercentage":"20"}, {...}, {...}]
附加说明:
1.请保持ajax调用w / o jQuery,因为我更熟悉这种格式
2. serverurl()
在全局js文件中定义,我已经确认它是正确的
答案 0 :(得分:0)
可能不完全有用,但如果您使用Chrome,则可以右键单击&gt;检查&gt;网络,然后打开这个你可以重新执行ajax页面,你会看到所有的http请求,它们的类型,状态和响应,当ajax完成后点击“停止录制”。您可以检查ajax GET请求的HTTP标头,看看它们是否符合预期。如果它们没问题,问题必须在php / server上。
答案 1 :(得分:0)
不正确的SQL语句,应该是
&#34; SELECT vendorname,vendortype,rebatepercentage FROM Vendors WHERE Vendorid =&#34;。$ vendorid);