我正在尝试使用 jQuery's .ajax() 查询包含“票”信息的数据库方法...
$.ajax({
type: 'GET',
url: 'Preview.ashx',
data: 'ticketID=' + ticketID,
success: function (data) {
// 'data' should be a row from the database, so it should be like an
// array that contains each column of the row
// do stuff with this data
}
});
...以便一切正常。我在使用 data 变量时遇到问题。在服务器端,我做...
// get the ticket ID from the POST parameter
int ticketID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1;
if (ticketID >= 0) {
// grab the data from the database, getInfo() will retrieve the row
// in the DB that corresponds to the ticket ID given, returning an
// ArrayList with all of the information
ArrayList theTicket = getInfo(context, ticketID);
// now, I need to somehow return this information so that I could deal with it
// in the 'success' callback function above
return;
} else {
// something went wrong with the 'newTicket' POST parameter
context.Response.ContentType = "text/plain";
context.Response.Write("Error with 'ticketID' POST parameter. \n");
return;
}
return;
我对此进行了足够的调试,以确保 ArrayList 包含正确的信息。现在我只需要归还它。
我该怎么做?我将如何返回 ArrayList 中的数据?是否可以构建响应,以便我可以在回调函数中执行 data.ID、data.otherColumnName 等...以访问不同的字段?
请您参考如下方法:
是的,这是可能的。看看JSON .您可以使用 JavascriptSerializer或 DataContractJsonSerializer以 JSON 格式序列化对象的类。另请查看 this link
您可以使用 ContentType application/json 并且 body 将是上述序列化程序之一将返回的字符串。您可以使用 else 语句中的相同代码
