我尝试使用xmlhttprequest向php响应器发送对象,运行搜索查询,然后将结果作为对象返回,但由于某种原因没有结果。我可以在network选项卡下看到响应者生成了所需的记录,但该记录未被处理。我无论如何也看不出这个问题。
请求:
function returnJSON(variable, URL, callback) {
var ajaxObj = new XMLHttpRequest();
ajaxObj.open("POST", URL, true);
console.log("posting");
ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxObj.onreadystatechange = function() {
if (ajaxObj.status === 200)
if (ajaxObj.readyState === 4)
callback(JSON.parse(ajaxObj.responseText));
console.log(ajaxObj.responseText);
};
ajaxObj.send(variable);
}
使用所述回调的示例函数。
function getSearch(e){
e.preventDefault();
var prodCode = document.getElementById("productCode").value;
var productDetails = "productCode="+prodCode;
returnJSON(
productDetails,
'api/database/returnSearch.php',
function(data) {
getSearchResult(data);
console.log("working");
}
);
if (searchResults.length > 1) {
alert("There are too many results to display.");
}else if (searchResults.length = 0){
alert("There are no results for "+prodCode);
}
else if (searchResults.length > 0){
document.getElementById("pCode").value = searchResults[0][0];
document.getElementById("productN").value = searchResults[0][1];
document.getElementById("description").value = searchResults[0][2];
document.getElementById("productType").value = searchResults[0][3];
document.getElementById("price").value = searchResults[0][4];
document.getElementById("quantity").value = searchResults[0][5];
}
// setAdmin();
}
有问题的php响应器。
<?php
global $range;
$range = [];
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
if(isset($_POST['productCode'])){
$prodCode= $_POST['productCode'];
$productCode = NULL;
$prodName = NULL;
$desc = NULL;
$prodType = NULL;
$price = NULL;
$quantity = NULL;
$db = new PDO("mysql:host=$hostname;dbname=webcw", $username, $password);
$sql = "SELECT productCode, productName, productType, description, price, quantity
FROM product
WHERE productCode = '$prodCode';";
foreach ($db->query($sql) as $row) :
$productCode = $row ['productCode'];
$prodName = $row ['productName'];
$desc = $row ['description'];
$prodType = $row ['productType'];
$price = $row ['price'];
$quantity = $row ['quantity'];
$product = array($prodCode, $prodName, $desc, $prodType, $price, $quantity);
$range[] = $product;
endforeach;
echo json_encode($range);
$db = null;
}
?>
你们有人能帮上忙吗?
提前感谢您的帮助。
发布于 2015-03-23 19:11:06
替换以下代码行:
if (ajaxObj.status === 200)
if (ajaxObj.readyState === 4)
callback(JSON.parse(ajaxObj.responseText));
console.log(ajaxObj.responseText);有了这些:
if (ajaxObj.readyState === 4) {
if (ajaxObj.status === 200){
callback(JSON.parse(ajaxObj.responseText));
console.log(ajaxObj.responseText);
}
}发布于 2015-03-24 19:20:50
我设法让它工作起来
function returnJSON(variable, URL, callback) {
var ajaxObj = new XMLHttpRequest();
ajaxObj.open("POST", URL, true);
console.log("posting");
ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxObj.addEventListener("load",
function() {
console.log("received");
callback(JSON.parse(ajaxObj.responseText));
}
);
ajaxObj.send(variable);
}
https://stackoverflow.com/questions/29208578
复制相似问题