Parse在AJAX成功函数中返回了C#列表

时间:2015-11-03 20:43:13

标签: c# jquery ajax webforms

我正在使用网络表单,我在我的ajax函数调用后面的代码中有一个web方法。看来webmethod正在返回数据,但我在将返回的json字符串解析为表行时遇到问题

这是我的代码隐藏方法(Web方法):

public class Part
{
    public string Number { get; set; }
    public string Price { get; set; }
    public string NetAvailable { get; set; }
    public string Location { get; set; }
    public string Distance { get; set; }
    public string Phone { get; set; }
}

[WebMethod]
public static List<Part> GetParts(string partnumber)
{
    List<Part> parts = new List<Part>();

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NXT"].ToString()))
    {
        con.Open();
        using (SqlCommand command = new SqlCommand("SELECT prod, qtyonhand, whse, listprice FROM icsw where prod LIKE @partnumber", con))
        {
            command.Parameters.AddWithValue("@partnumber", "%" + partnumber + "%");

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Part part = new Part
                    {
                        Number = reader[0].ToString(),
                        Price = reader[3].ToString(),
                        NetAvailable = reader[1].ToString(),
                        Distance = "0.0",
                        Location = reader[2].ToString(),
                        Phone = GetBranchPhoneNumber(reader[2].ToString())
                    };
                    parts.Add(part);

                }
            }
        }
    }

    return parts;
}

这是我的AJAX电话:

$("#searchbtn").click(function() {
    $.ajax({
        url: 'partslocator.aspx/GetParts',
        type: 'POST',
        data: JSON.stringify({ 'partnumber': $("#searchtbx").val()}),
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {

            for (var i in data) {
                alert(JSON.stringify(data[i]));
                $('#searchresultstble').append("<tr><td>" + data[i].Number + "</td><td>" + data[i].Price + "</td><td>" + data[i].NetAvailable + "</td><td>" + data[i].Location + "</td><td>" + data[i].Distance + "</td><td>" + data[i].Phone + "</td></tr>");
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});

以下是数据:

{"d":[{"__type":"partslocator+Part","Number":"0-2809 1025","Price":"2.50000","NetAvailable":"0.00","Location":"82pk","Distance":"0.0","Phone":"test"},{"__type":"partslocator+Part","Number":"0-2809 1025","Price":"2.58000","NetAvailable":"0.00","Location":"Phx","Distance":"0.0","Phone":"test"}]}

为什么我的所有行都未定义?

1 个答案:

答案 0 :(得分:3)

您将注意到,返回的data实际上不是数组,而是包含该数组的值(d)的对象。试试这个:

    success: function (returnedData) {
        var data = returnedData.d;
        for (var i in data) {
            alert(JSON.stringify(data[i]));
            $('#searchresultstble').append("<tr><td>" + data[i].Number + "</td><td>" + data[i].Price + "</td><td>" + data[i].NetAvailable + "</td><td>" + data[i].Location + "</td><td>" + data[i].Distance + "</td><td>" + data[i].Phone + "</td></tr>");
        }
    },