我有一个使用mysql从php返回数组对象的函数,我想使用javascript从ajax函数调用数组,但我不知道如何在javascript动态表或控制台日志中显示数组对象php....
我的PHP文件:
$age = $_POST["param"]; //value ajax param
$list = BussinesLayer::getUsers($age); //load my list
//list properties ej: list->getName(), getAge(), getOcupation()->getDescription(), etc..
echo json_encode($list);
我的js函数:
fnListUsers: function() {
var agedata = $("#txtAge").val();
$.ajax({
type : "POST",
url : "../ControllerFile/SearchUsers.php",
data : {"param" : agedata},
dataType : 'json',
success : global.fnSuccessList,
error : function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
}
});
},
fnSuccessList: function(data) {
var array = JSON.parse(data); // -> dont work Uncaught SyntaxError: Unexpected token o in JSON at position 1
var array2 = jQuery.parseJSON(data); // dont work Uncaught SyntaxError: Unexpected token < in JSON at position 0
//how display my arrayobject ?
console.log(data.getName, data.getOcupation.getDescripition);
}
最佳答案
您的成功函数已经具有 json 格式的数据
。所以你不需要再用JSON解析它。只需使用 console.log(data)
即可查看您的对象。
fnSuccessList: function(data) {
var array = JSON.parse(data); // remove this
var array2 = jQuery.parseJSON(data); // remove this
console.log(data);
}
关于javascript - 如何将数组对象php显示到ajax js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481030/