编程语言
首页 > 编程语言> > 解析AJAX成功函数中返回的C#列表

解析AJAX成功函数中返回的C#列表

作者:互联网

我正在使用Web表单,而我的Ajax函数正在调用的代码背后有一个Web方法.看来web方法正在返回数据,但是我在将返回的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"}]}

为什么对所有要添加的行都无法定义?

解决方法:

您会注意到,返回的数据实际上不是数组,而是具有包含该数组的值(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>");
        }
    },

标签:ajax,webforms,c,jquery
来源: https://codeday.me/bug/20191119/2036564.html