未定义的值 document.getElementById
Undefined value document.getElementById
我这里有个问题。我想使用我已经创建的提供程序创建一个 Web 服务请求程序。问题是我总是得到 ID、Kab 和 ID2 的未定义值。有人可以帮我修复请求者中的代码吗?
这是提供商
$sql="SELECT * FROM mytable";
$query = mysqli_query($con, $sql);
$result = array();
while($row = mysqli_fetch_array($query)){
array_push($result, array(
'IDKabupaten' => $row['IDKabupaten'],
'Kabupaten' => $row['Kabupaten'],
'attribut' => array(
0 => array(
'IDProvinsi' => $row['IDProvinsi']
)
)
));
}
echo json_encode(array('mytable' => $result));
?>
这是请求者
<script type="text/javascript">
$.getJSON("http://localhost/json2/wsp_coba2.php", function(result){
console.log(result);
$.each(result,function(i)
{
document.getElementById("DataKabupaten").innerHTML +="ID :" + result[i].IDKabupaten +"<br>Kab :" + result[i].Kabupaten
+"<br>ID2 :" + result[i].IDProvinsi + "<br><br>";
});
});
</script>
我得到
ID : undefined
Kab : undefined
ID2 : undefined
试试这个...
$.each(result,function(i)
{
document.getElementById("DataKabupaten").innerHTML +="ID :" + result[i].IDKabupaten +"<br>Kab :" + result[i].Kabupaten
+"<br>ID2 :" + result[i].IDProvinsi + "<br><br>";
});
result.forEach((singleResult, index) => {
// and then do your thing here. I don't use jquery but I took a quick look at their docs and your loop doesn't look right. This is the same function that jquery is calling under the hood.
})
删除你的代码...我只是把它留在那里所以你可以比较。
const result = { "mytable": [ { "IDKabupaten": "3301", "Kabupaten": "CILACAP", "attribut": [ { "IDProvinsi": "33" } ] }, { "IDKabupaten": "3302", "Kabupaten": "BANYUMAS", "attribut": [ { "IDProvinsi": "33" } ] } ] };
$.each(result.mytable,function(i) {
console.log(result.mytable[i].IDKabupaten);
$.each(result.mytable[i].attribut, function (j) {
console.log(result.mytable[i].attribut[j].IDProvinsi)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
你可以看看
我这里有个问题。我想使用我已经创建的提供程序创建一个 Web 服务请求程序。问题是我总是得到 ID、Kab 和 ID2 的未定义值。有人可以帮我修复请求者中的代码吗?
这是提供商
$sql="SELECT * FROM mytable";
$query = mysqli_query($con, $sql);
$result = array();
while($row = mysqli_fetch_array($query)){
array_push($result, array(
'IDKabupaten' => $row['IDKabupaten'],
'Kabupaten' => $row['Kabupaten'],
'attribut' => array(
0 => array(
'IDProvinsi' => $row['IDProvinsi']
)
)
));
}
echo json_encode(array('mytable' => $result));
?>
这是请求者
<script type="text/javascript">
$.getJSON("http://localhost/json2/wsp_coba2.php", function(result){
console.log(result);
$.each(result,function(i)
{
document.getElementById("DataKabupaten").innerHTML +="ID :" + result[i].IDKabupaten +"<br>Kab :" + result[i].Kabupaten
+"<br>ID2 :" + result[i].IDProvinsi + "<br><br>";
});
});
</script>
我得到
ID : undefined
Kab : undefined
ID2 : undefined
试试这个...
$.each(result,function(i)
{
document.getElementById("DataKabupaten").innerHTML +="ID :" + result[i].IDKabupaten +"<br>Kab :" + result[i].Kabupaten
+"<br>ID2 :" + result[i].IDProvinsi + "<br><br>";
});
result.forEach((singleResult, index) => {
// and then do your thing here. I don't use jquery but I took a quick look at their docs and your loop doesn't look right. This is the same function that jquery is calling under the hood.
})
删除你的代码...我只是把它留在那里所以你可以比较。
const result = { "mytable": [ { "IDKabupaten": "3301", "Kabupaten": "CILACAP", "attribut": [ { "IDProvinsi": "33" } ] }, { "IDKabupaten": "3302", "Kabupaten": "BANYUMAS", "attribut": [ { "IDProvinsi": "33" } ] } ] };
$.each(result.mytable,function(i) {
console.log(result.mytable[i].IDKabupaten);
$.each(result.mytable[i].attribut, function (j) {
console.log(result.mytable[i].attribut[j].IDProvinsi)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
你可以看看