【发布时间】: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