success: function( jsondata, textStatus, jQxhr ){
$.each(jsondata.data, function(key,value){
$("#student").append("<option value=" + value.id + ">" + value.name + "</option>");
});
我正在尝试从数据库中获取数据并将其添加到下拉框中。问题是我遵循的任何指南我都会收到此错误:
jquery-1.11.3.min.js:2未捕获的TypeError:无法读取未定义的属性'length'
我出错的任何想法?
更新 没有走得很远
PHP:
$sql = $dbh->prepare("SELECT CustomerID, FirstName FROM customer"); $sql->execute(); $row = $sql->fetchAll(); //var_dump ($row); //$para = implode(" ",$row); echo json_encode($row); //echo json_encode(array('data'=>$result));
使用Javascript:
$.each(jsondata.data, function(key,value){ $("#student").append( "<option value=" + value.CustomerID +">"+value.FirstName+"</option>" ); });
控制台:
[{"CustomerID":"2","0":"2","FirstName":"John","1":"John"},{"CustomerID":"3","0":"3","FirstName":"William","1":"William"}]
未捕获的TypeError:无法读取未定义的属性“长度”