Javascript 如何将数组从php传递到ajax成功函数?

Javascript 如何将数组从php传递到ajax成功函数?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在开发一个网络应用程序。我正在从数据库中检索数据,并使用$row=mysqli\u fetch\u array()将其存储到一个数组中。问题是,这个数组应该使用echo json\u encode($array)转到ajax,但是我不知道如何从ajax成功函数接收它,顺便说一下,我使用的是jQuery.parseJSON()。之后,我将把该数组中的值附加到元素中 get_AssetModel.php: $sqlGetAssetModels = "select * from tblbrands

我正在开发一个网络应用程序。我正在从数据库中检索数据,并使用
$row=mysqli\u fetch\u array()
将其存储到一个数组中。问题是,这个数组应该使用
echo json\u encode($array)
转到
ajax
,但是我不知道如何从ajax成功函数接收它,顺便说一下,我使用的是
jQuery.parseJSON()
。之后,我将把该数组中的值附加到
元素中

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

或者是另一种方式?

我假设您的
数据可能如下例所示:

var数据=[“Anthony”、“Jhake”、“Ravelo”、“Antigro”];
data.forEach(函数(值、索引){
var option=document.createElement(“选项”);
option.text=值;
option.value=指数;
var select=document.getElementById(“txtInvModel”);
选择.appendChild(选项);
});

选择一个

请分享您如何在响应块中表示数据。想知道
jQuery.parseJSON(数据)数据。这是一个包含对象的数组?或者只是一个值数组。它只是一个值数组。我在想有命名索引的对象,但我认为它不起作用。什么不起作用?使用浏览器的DevTools/Network选项卡查看返回的结果。或者在
success
函数中,使用
console.table(data)
打印使用的数据。哇。谢谢你这个好主意。我要试试这个,先生。但是,从我的数据库检索数据怎么样?我是否也应该改变在$models[]=$row[3]中构造代码的方式?如果$row[3]包含一组上下文,那么您不必在那里做任何事情。