duanji4449 2013-11-14 15:35
浏览 53
已采纳

JSON对象在控制台中显示对象,但也包含php脚本返回的字符串中的'undefined'

I have a php script that queries my database for some values and puts them into a json string.

<?php

if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
    include_once("../php_scripts/connect.php");

    $arr = array();
    if ($result = $mysqli->query("SELECT due_date FROM payment_due_dates WHERE client_id = '" . $_POST['name']. "'")) {
        $rowCount = $result->num_rows;

        echo '[';
        $i = 0;
        while ($row = $result->fetch_assoc()) {
            if ($i <= $rowCount - 2){
                echo json_encode($row);
                echo ',';
            }else{
                echo json_encode($row);
            }
            $i++;
        }
        $result->free();
    }
    echo ']';   
}

?>

Then jquery converts into a json object and adds the values to a select box.

$(document).ready(function(){
          $('#subName').click(function(){

                var name = $('input#txtName').val();

                if($.trim(name) != ''){
                    $.post('ajax/load_patient_episodes.php', {name: name}, function(data){
                    console.log(data);
                    var obj = $.parseJSON(data);
                    console.log(obj);
                        var output;
                        $.each(obj, function(key, val){
                            output += '<option>' + val.due_date + '</option>';
                        });
                        $('#selEpisode2').html("<option>" + output + "</option>");
                    });
                }
          });
        });

It loads the values but also gives this error in my console log:

event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5

and, when it adds the values to the selectbox it also adds the text 'undefined'. So the options in my select box appear as:

undefined

2013-11-13

2013-11-13

2013-11-13

I'm assuming it may have something to do with the proto array that is in there but I'm not sure how it got there. Still somewhat a newbie on this stuff. Here is my console log. Any ideas how I can lose the 'undefined in my select options?' Also, is there a better way I should be going about loading select options from a php query?

event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5
[Object, Object, Object]
0: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
  • 写回答

3条回答 默认 最新

  • dongpu1881 2013-11-14 15:44
    关注

    Try this

    var output = '';
    $.each(obj, function(key, val){
      if (typeof (val.due_date) != undefined) { 
        output += '<option>' + val.due_date + '</option>';
      }
    });
    

    You might also want to SELECT UNIQUE due_date.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题