没有带有xmlhttprequest的responseText

时间:2015-03-23 11:00:42

标签: javascript php ajax object

我试图使用xmlhttprequest向php响应者发送一个对象,运行搜索查询,然后将结果作为对象发回,但由于某种原因,没有结果。我可以在网络选项卡下看到响应者生成了所需的记录但是没有被处理。我不能为我的生活看到问题。

请求:



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;
  }
  ?>
&#13;
&#13;
&#13;

你们有人可以帮忙吗?

提前感谢您的任何帮助。

2 个答案:

答案 0 :(得分:0)

替换这些代码行:

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);
   }
}

答案 1 :(得分:0)

我设法让它与

一起工作

&#13;
&#13;
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);
}
&#13;
&#13;
&#13;