Webservice返回正确的数据,但解析为未定义

时间:2016-01-14 10:04:35

标签: javascript c# web-services

我有这个代码,用于填写我网站上的下拉框。它从数据库中提取一些部门,然后将部门的ID作为选项值,然后将部门命名为选项" name" (它显示的名称)

/**
 * This function is Run when creating the dropdown to choose a department
 * when creating a new activity.
 */
function CreateNewActivityDepartmentDropdown() {
    $.getJSON("http://localhost/GetDepartmentsForDropdown", function (data) {
        var object = $.parseJSON(data);
        var dropdown = document.getElementById("new-activity-modal-department-dropdown");
        for (var department in object) {
            if (object.hasOwnProperty(department)) {
                var array = department.split(',');
                var option = document.createElement("option");
                option.id = array[0];
                option.innerHTML = array[1];
                dropdown.add(option);
            }
        }
    });
}

对象确实包含正确的数据。我用Google Chromes调试器检查了它。数据由id,department字符串组成,在我的for循环中,我尝试拆分我得到的字符串,但首先,每个选项的值似乎只是一个增量值(0-n)而不是id&#39 ; s我有(1-12),选项文本是"未定义"。

当我调试时,我发现似乎ID和Text似乎不存在。我在这里错过了什么?在JavaScript开发方面仍然相当新鲜:)

修改

添加了console.log(data, object)

的输出
["1,Commercial","2,Economy","3,Technical Dept.","4,IT","5,Flight Op","6,Legal","7,Ground Op","8,BDO","9,Administration","10,Training","11,Passenger Service","12,Cabin"] Array[12]

编辑2

从Web服务接收数据对象。 Web服务是用C#编写的,我用来获取它的代码如下:

public static List<String> GetDepartmentDropdownList()
{
    List<String> departments = new List<String>();
    using (SqlConnection conn = GetSqlConnection())
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "SELECT * FROM dbo.Department WHERE is_used != 0";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;

            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Int32 id = reader.GetInt32(0);
                    String department = id + "," + reader.GetString(1);
                    departments.Add(department);
                }
            }

        }
    }

    return departments;
}

以下是该网站所称的内容:

public String GetDepartmentsForDropdown()
{
    List<String> departments = Facade.GetDepartmentDropdownList();

    return JsonConvert.SerializeObject(departments);
}

2 个答案:

答案 0 :(得分:1)

getJSON在用data填充之前将HTTP响应正文解析为JSON。

var object = $.parseJSON(data);会将数组转换为字符串,然后尝试将其解析为JSON(因为它不是JSON会失败)。别这么做。

答案 1 :(得分:0)

对于那些感兴趣的人,我最终得到的解决方案是:

/**
 * This function is Run when creating the dropdown to choose a department
 * when creating a new activity.
 */
function CreateNewActivityDepartmentDropdown() {
    $.getJSON("http://localhost/GetDepartmentsForDropdown", function (data) {
        var object = $.parseJSON(data);
        var dropdown = document.getElementById("new-activity-modal-department-dropdown");
        for (var i = 0; i < object.length; i++) {
            var array = object[i].split(',');
            var option = document.createElement("option");
            option.value = array[0];
            option.innerHTML = array[1];
            dropdown.add(option);
        }
    });
}

不需要C#代码编辑。我不知道Quentin给我的解决方案,但似乎没有做这一步:

var object = $.parseJSON(data);

我无法让它发挥作用。我很乐意给出答案,但可以解释一下。