【问题标题】:jQuery AJAX postback to WebMethod returning only first row of resultsjQuery AJAX 回传到 WebMethod 只返回第一行结果
【发布时间】:2012-02-07 16:14:20
【问题描述】:

我有以下 JQUERY 代码,我希望将 3 个 json 结果返回到控制台。发生的事情是我得到了第一次返回结果的 3 个副本。

例如,我试图查看文件名,然后我返回的是“名字、名字、名字”而不是“名字、名字、名字”

这是我的代码:

$.ajax({
    type: "POST",
    url: "front.aspx/GetData",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        var obj = msg.d;
        $.each(obj, function(index, value){ 
            console.log(value);    
        });
    }
});

每个函数我做错了什么?

我认为你不需要我的 CS 代码来告诉我我做错了什么,但如果你这样做了,那就是:

public class LoadData {
    public string filename;
    public string date;
    public string filetype;
    public Int32 height;
    public Int32 width;
    public string uploadGroup;
    public string title;
    public string uniqueID;
    public string uploader;
    public string uniqueIDnoExt;
}

[WebMethod]
public static List<LoadData> GetData() {
    LoadData b = new LoadData();
    List<LoadData> info = new List<LoadData>();
    SqlDataReader reader;
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();

    SqlCommand command = new SqlCommand("SELECT * FROM uploads ORDER BY id DESC", connection);
    command.Parameters.Add(new SqlParameter("uploader", "anonymous"));

    reader = command.ExecuteReader();

    while (reader.Read()) {
    b.filename = reader.GetString(1);
    b.date = reader.GetSqlDateTime(3).ToString();
    b.filetype = reader.GetString(4);
    b.height = (Int32)reader.GetSqlInt32(5);
    b.width = (Int32)reader.GetSqlInt32(6);
    b.uploadGroup = reader.GetString(7);
    b.title = reader.GetString(8);
    b.uniqueID = reader.GetString(9);
    b.uploader = reader.GetString(10);
    b.uniqueIDnoExt = reader.GetString(12);

    info.Add(b);
    }

    return info;
}

【问题讨论】:

    标签: c# jquery asp.net ajax json


    【解决方案1】:

    移动这条线

    LoadData b = new LoadData();
    

    在循环内部。

    while (reader.Read()) {
    LoadData b = new LoadData();
    
    b.filename = reader.GetString(1);
    b.date = reader.GetSqlDateTime(3).ToString();
    b.filetype = reader.GetString(4);
    b.height = (Int32)reader.GetSqlInt32(5);
    b.width = (Int32)reader.GetSqlInt32(6);
    b.uploadGroup = reader.GetString(7);
    b.title = reader.GetString(8);
    b.uniqueID = reader.GetString(9);
    b.uploader = reader.GetString(10);
    b.uniqueIDnoExt = reader.GetString(12);
    
    info.Add(b);
    }
    

    您现在拥有它的方式只制作一行数据。 List&lt;&gt; 保存引用,它不会重新创建它们。因此,如果您不制作新数据,则不会插入任何新数据(就像现在一样)。您只是在内存中添加第一个,然后更改值。

    您还可以阅读: http://en.wikipedia.org/wiki/Linked_list

    顺便说一句。

    你已经打开了很多东西,你很快就会没有资源。在需要关闭的对象上使用using 关键字。而且,为了速度,在连接字符串上使用静态字符串(只读)。

    【讨论】:

      【解决方案2】:

      您以错误的方式创建列表.. 按照 @Aristos 的方式正确添加对象。

      并在您的 ajax 请求 sucess: 部分..

      循环d.data。检查这个JQuery Ajax with Class Arrays

      for (var i in d.data) {       }  
      

      你可以Manually convert result dataset to JSON

      【讨论】:

        猜你喜欢
        • 2018-01-09
        • 2011-06-09
        • 1970-01-01
        • 2014-04-17
        • 2016-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多