简体   繁体   English

没有带有xmlhttprequest的responseText

[英]No responseText with xmlhttprequest

I'm attempting to use a xmlhttprequest to send a php responder an object, run a search query and then send back the result as an object but for some reason there's no result. 我正在尝试使用xmlhttprequest向php响应器发送一个对象,运行搜索查询,然后将结果作为对象发送回,但是由于某种原因,没有结果。 I can see under the network tab that the responder produces the desired record but its not being handled. 我可以在“网络”标签下看到响应者生成了所需的记录,但记录未得到处理。 I can't for the life of me see the problem. 我一辈子都看不到问题。

The request: 要求:

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

A example function that utilises said callback. 利用所述回调的示例函数。

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

The php responder in question. 有问题的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; } ?> 

Can any of you help? 你们有什么可以帮助的吗?

Thanks in advance for any assistance. 在此先感谢您的协助。

Replace these code lines: 替换这些代码行:

if (ajaxObj.status === 200)
  if (ajaxObj.readyState === 4)
    callback(JSON.parse(ajaxObj.responseText));
    console.log(ajaxObj.responseText);

with these: 用这些:

if (ajaxObj.readyState === 4) {
   if (ajaxObj.status === 200){
      callback(JSON.parse(ajaxObj.responseText));
      console.log(ajaxObj.responseText);
   }
}

I managed to get it working with 我设法使其与

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM