我正在开发一个网络应用程序。我正在从数据库中检索数据并使用$row = mysqli_fetch_array(). 问题是,这个数组应该去ajax使用,echo json_encode($array)但我不知道如何从 ajax 成功函数接收它,顺便说一句,我正在使用jQuery.parseJSON(). 之后,我将把该数组中的值附加到一个<option>元素上。
get_AssetModel.php:
$sqlGetAssetModels = "select * from tblbrands where brand_name like '$phpGetAssetModels'";
//$array = array();
$models = array();
if ($result = mysqli_query($cn, $sqlGetAssetModels)) {
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
$models[] = $row[3];
}
} else {
}
echo json_encode($models);
将接收数组的 javascript 文件:
$.ajax({
url: "inventory/get_AssetModel.php",
type: "POST",
data: chAssBrand,
success: function(data, textStatus, jqXHR){
var data = jQuery.parseJSON(data);
var node = document.createElement("option");
var textnode = document.createTextNode(data.0);
node.appendChild(textnode);
document.getElementById("txtInvModel").appendChild(node);
},
error: function(jqXHR, textStatus, errorThrown){
swal("Error!", textStatus, "error");
}
});
或者是他们的其他方式?
陪伴而非守候