我有 ASP.NET 站点。在某一时刻触发回发,并在服务器上修改一些数据并发送响应(使用新的 View 状态)。 问题是我使用 jQuery 只在页面上显示该响应的一部分。这行得通,但我在用新值更新 View 状态时遇到问题。 我有这样的东西:
var updatePreviewArea = function (nid) {
var $content = jQuery('<div></div>');
$content.load('http://site.com?nid=' + nid, function (response) {
var $response = jQuery(response);
jQuery('targetDiv1').replaceWith($response.find('#srcDiv1'));
jQuery('targetDiv2').replaceWith($response.find('#srcDiv2'));
// update viewstate from postback response
var selectors = ['#__VIEWSTATE', '#__EVENTVALIDATION'];
for (var i in selectors) {
var value = $response.find(selectors[i]).val();
jQuery(selectors[i]).val(value);
}
});
}
但在此之后我得到:
Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.' when calling method: [nsIDOMEventListener::handleEvent]
最佳答案
您正在获取从服务器返回的 View 状态并使用它更新您的内容页面。 ViewState 基本上是一组编码的名称/值对,需要与其相关页面上的内容相匹配。如果您获取一个页面的 ViewState 并将其粘贴到另一个页面,加密值将不匹配,您将收到与您所看到的类似的错误。
由于您使用 jQuery 和 AJAX 从您的服务器动态获取内容,我不确定您为什么要依赖 ViewState(但我不熟悉您的应用程序)。
关于javascript - 从回发响应更新 View 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9005090/