【发布时间】:2011-08-10 07:12:05
【问题描述】:
我正在使用来自this example 的 JSON 和 XML 创建 AJAX 服务。 在 service.cs 中我进行了更改:
[ServiceContract(Namespace = "XmlAjaxService")]
public interface ICalculator
{
...
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
int GetTimersCallCount();
}
public class CalculatorService : ICalculator
{
private System.Timers.Timer timer = null;
private int timerCalls = 0;
public CalculatorService()
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
public int GetTimersCallCount()
{
return this.timerCalls;
}
}
在页面 javascript 我这样做:
function GetTimersTick() {
// Create HTTP request
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("This sample only works in browsers with AJAX support");
return false;
}
}
}
// Create result handler
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById("result").value = xmlHttp.responseText;
document.getElementById("statustext").value = xmlHttp.getAllResponseHeaders();
}
}
// Build the operation URL
var url = "service.svc/";
url = url + "GetTimersCallCount";
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send();
}
但是当我按下这个功能的按钮时,我是从服务 0 得到的。出了什么问题?
【问题讨论】:
-
据我从您的示例代码中可以看出,您永远不会更新
timerCalls。 -
这是更新我想在这里显示此代码