javascript 类型错误:e 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18998690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:01:10  来源:igfitidea点击:

Type error : e is undefined

javascriptphpjquery

提问by Ajit

I am getting a type error: e undefined on the javascript code. I am trying to populate a dropdown list using jquery with data sent from a mysql server.

我收到一个类型错误:e 在 javascript 代码上未定义。我正在尝试使用 jquery 和从 mysql 服务器发送的数据填充下拉列表。

here is the javascript code

这是javascript代码

$(document).ready(function(){// This script uses jquery and ajax it is used to set the values in
$("#day").change(function(){// the time field whenever a day is selected.
    var day=$("#day").val();
    var doctor=$("#doctor").val();
    $.ajax({
        type:"post",
        url:"time.php",
        data:"day="+day+"&doctor="+doctor,
        dataType : 'json',
        success:function(data){
            var option = '';
            $.each(data.d, function(index, value) {
                console.log(data.d);
                option += '<option>' + value.arr + '</option>';
            });
            $('#timing').html(option);
        }

    });
});
});

Here's the php script which fetches data from a mysql database

这是从 mysql 数据库中获取数据的 php 脚本

$doctor = $_POST['doctor'];
$day = $_POST['day'];
$query="SELECT * FROM schedule WHERE doctor='$doctor' AND day='$day'";
$arr = array();
$result = mysqli_query($con, $query);
$i = 0; 
//Initialize the variable which passes over the array key values
$row = mysqli_fetch_assoc($result);    //Fetches an associative array of the row
$index = array_keys($row);             // Fetches an array of keys for the row.
while($row[$index[$i]] != NULL)
{
    if($row[$index[$i]] == 1) {
        //$res = $index[$i];
        //echo json_encode($res);
        array_push($arr, $index[$res]);
    }
    $i++;
}       

回答by Rohan Kumar

You are not using each()in a right way replace the line

您没有以正确的方式使用each()替换该行

$.each(data.d, function(index, value) {

by

经过

if(data.d)
{
    $(data.d).each(function(index, value) {
       // your code

Alsoin phpuse json_encode()to make your dataas json

同样php 中使用json_encode()您的数据设为json

} // end of while
echo json_encode($arr);

Updated code,try in your php script,

更新代码,在你的php 脚本中尝试,

$i = 0; 
//Initialize the variable which passes over the array key values
while($row = mysqli_fetch_assoc($result))
{
    $arr['d'][$i]=$row['doctor'];
    // you can add more fields in array like above
    $i++;
}
echo json_encode($arr);
return;

Andin your Javascriptit will work after reading DOC jquery.each()

在你的JavaScript它会读取DOC后工作jquery.each()

 $.each(data.d, function(index, value) {
      option += '<option>' + value+ '</option>';
 });

回答by Jacob Clark

It appears you're not parsing your JSON, try the following, note the line

看来您没有解析您的 JSON,请尝试以下操作,注意该行

data = JSON.parse(data);

data = JSON.parse(data);

You must use JSON.parse on your data object otherwise JavaScript will treat it as a string.

您必须在数据对象上使用 JSON.parse,否则 JavaScript 会将其视为字符串。

$(document).ready(function(){// This script uses jquery and ajax it is used to set the values in
$("#day").change(function(){// the time field whenever a day is selected.
  var day=$("#day").val();
  var doctor=$("#doctor").val();

  $.ajax({
      type:"post",
      url:"time.php",
      data:"day="+day+"&doctor="+doctor,
      dataType : 'json',
      success:function(data){
      var option = '';

      data = JSON.parse(data);

      $.each(data.d, function(index, value) {

      console.log(data.d);
          option += '<option>' + value.arr + '</option>';

         });

     $('#timing').html(option);
        }

       });