我想建立一个每秒更新一些状态文本的asp.net(c#)页面。 现在我已经实现了一个调用另一个PageMethod的按钮,它可以重新启动某些内容并需要一些时间。问题是,当我打电话重启PageMethod的,更新PageMethod的不能只要重启方法继续更新......asp.net从Javascript调用WebMethod asyncronous
我写了一个小例子来说明我的意思:
的WebMethods在我的页面:
[WebMethod]
public static string Update()
{
//return "a" to see when the Update PageMethod succeeded
return "a";
}
[WebMethod]
public static string Restart()
{
//the restart will take a while
Thread.Sleep(2000);
//return "a" to see when the Restart PageMethod succeeded
return "a";
}
HTML元素更新:
<p id="update" style="float:left;"></p>
<p id="restart" style="float:right;"></p>
的PageMethod的来电:
callUpdate()
function callUpdate() {
PageMethods.Update(function (text) {
//itself+text from pagemethod
$('#update').text($('#update').text() + text);
});
setTimeout(callUpdate, 1000);
}
callRestart()
function callRestart() {
PageMethods.Restart(function (text) {
//itself+text from pagemethod
$('#restart').text($('#restart').text() + text);
});
setTimeout(callRestart, 1000);
}
注意:更新也被称为每一秒都完成之后,只是为了看看它是如何工作
为了澄清:我想PageMethods独立执行到其他PageMethod的完成。
http://msdn.microsoft.com/en-us/library/aa480516.aspx
但我不认为这是我需要什么 我真的不知道如何称呼,从(?)使用Javascript(的BeginXXX和Endxxx)
* 编辑:*
关于到的Massimiliano Peluso的,js的ç颂诗是这样的:
callUpdate()
function callUpdate() {
$.ajax({
type: "POST",
url: "ServicePage.aspx/Update",
data: "{}",
contentType:
"application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#update').text($('#update').text() + msg.d);
}
});
setTimeout(callUpdate, 1000);
}
callRestart()
function callRestart() {
$.ajax({
type: "POST",
url: "ServicePage.aspx/Restart",
data: "{}",
contentType:
"application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#restart').text($('#restart').text() + msg.d);
}
});
setTimeout(callRestart, 1000);
}
注:当我用新的JS运行页面时,恰好有同样的问题之前:直到重新启动方法完成的更新方法无能为力。
是'msg'一个JSON对象?我尝试了'var jsonObj = $ .parseJSON(msg);警报(jsonObj);'我得到一个警告** null ** – Sonnenhut
'味精。d'已经这样做了,'d'是因为.NET Framework的xss预防。 – Sonnenhut