这个jquery代码用于通过ADO.net从数据库中检索数据:
$(document).ready(getBooks);
function getBooks() {
$.ajax({
type: "POST",
url: "main.aspx/getBooks",
data: "{}",
datatype: "json",
contenttype: "application/json; charset=utf-8",
success: function (data) {
var TableContent = "<table>" +
"<tr>" +
"<td>ISBN</td>" +
"<td>Book Title</td>" +
"<td>Public Year</td>" +
"<td>Category</td>" +
"</tr>";
for (var i = 0; i < data.d.length; i++) {
TableContent += "<tr>" +
"<td>" + data.d[i].ISBN + "</td>" +
"<td>" + data.d[i].BOOK_TITLE + "</td>" +
"<td>" + data.d[i].PUBLICATION_YEAR + "</td>" +
"<td>" + data.d[i].CATEGORY_TYPE + "</td>" +
"</tr>";
}
TableContent += "</table>";
$("#UpdatePanel").html(TableContent);
},
error: $("#UpdatePanel").html("Error")
});
}
C#代码如下所示:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<Book> getBooks()
{
List<Book> allBooks = new List<Book>();
using (LibraryEntities le = new LibraryEntities())
{
allBooks = le.Books.ToList();
}
return allBooks;
}
我的问题是,在我添加之后总是一无所获:
error: $("#UpdatePanel").html("Error")
在UpdatePanel元素中只有“错误”,我是一个AJAX新手,需要你的帮助。