我正在尝试减少对服务器发出的获取请求的数量。我想将部分视图$.get
作为字符串返回给我的方法,这样我就可以一次返回其中的 3 个,而不是有 3 个单独的服务器回调。有没有办法做到这一点。到目前为止我有
到目前为止它只返回 System.Web.Mvc.PartialViewResult
function updateChat() {
/** I want to make this work so I only have 1 callback */
$.get("Home/refreshChatParts", {}, function (response) {
var html = response.split(",");
$("#chatWindow").replaceWith(html[0]);
$("#chatHeader").replaceWith(html[1]);
$("#playerList").replaceWith(html[2]);
});
/** this stuff works, but is a lot of call backs */
$.get("Home/refreshChatText", {}, function (html) {
$("#chatWindow").replaceWith(html);
});
$.get("Home/refreshChatHeader", {}, function (html) {
$("#chatHeader").replaceWith(html);
});
$.get("Home/refreshPlayerList", {}, function (html) {
$("#playerList").replaceWith(html);
});
*/
$.get("Home/getRequest", {}, function (requestUser) {
if (requestUser.toString() != "failed") {
pause = true;
var join = confirm(requestUser + " would like to play a game with you");
if (join) {
startGame(requestUser);
} else
pause = false;
resetRequestUser(requestUser);
}
});
//$.get("Home/checkForGameStart", {}, function (response) { /*alert(response);*/ });
scrollChatDown();
setTimeout('updateChat()', 20000);
}
public String refreshChatParts() {
//refresh chat text
ViewData["ChatText"] = getFormatedChatText();
//refresh usercount
try {
userCount = getUserCount();
ViewData["numberOfUsers"] = "There are " + userCount + " players online";
} catch (Exception e) { ViewData["numberOfUsers"] = e.Message; }
//refresh player list
ViewData["listOfPlayers"] = getFormatedPlayerList();
checkForGameStart();
return PartialView("ChatText") + "," + PartialView("ChatHeader") + "," + PartialView("playerList");
}