我正在为一个asp.net处理程序编写一个客户端,该处理程序处理并向轮询客户端发送通知。我每10秒钟用jquery进行ajax调用,等待通知。问题是每次我将响应发送回客户端时它总是调用错误回调。使用fiddler,我看到json响应以状态200到达客户端,但在firebug中,请求一直在等待。
客户端:
function poll(){
$.ajax({
url: "http://localhost:53914/NotificationChecker.ashx",
data: { userName: "ggyimesi" },
type: 'POST',
success: function(data){
alert("asd");
$('div#mydiv').text(data.Text);
},
complete: poll,
error: function(data){alert(data.status);},
timeout: 15000 });
}
$(document).ready(function(){
poll();}
);
服务器:
Response response = new Response();
response.Text = this.MessageText;
response.User = Result.AsyncState.ToString();
string json = JsonHelper.JsonSerializer<Response>(response);
Result.Context.Response.ContentType = "application/json";
Result.Context.Response.Write(json);
答案 0 :(得分:1)
我实际上发现了这个问题。问题是我从我的本地计算机上启动了客户端html而不是托管在Web服务器上并导致了问题。将其添加到本地Web服务器后,原始代码按原样运行。
答案 1 :(得分:0)
我认为你的ajax电话缺少以下内容:
dataType: 'json',
contentType: "application/json; charset=uft-8",
答案 2 :(得分:0)
试试这个:
$.ajax({
url: "/NotificationChecker.ashx",
dataType: 'json',
contentType: "application/json; charset=uft-8",
data: { userName: "ggyimesi" },
type: 'POST',
success: function(data){
alert("asd");
$('div#mydiv').text(data.Text);
},
complete: poll,
error: function(data){alert(data.status);},
timeout: 15000 });
}
答案 3 :(得分:0)
我认为您的问题是您通过了这样的用户名
data: { userName: "ggyimesi" }
我认为你应该这样做
data: '{userName:"' + ggyimesi + '"}'
你可以添加更多这样的
type: "POST",
url: "NotificationChecker.ashx",
contentType: "application/json; charset=utf-8",
dataType: 'json',