【发布时间】:2012-10-05 09:48:20
【问题描述】:
我正在为 asp.net 处理程序编写一个客户端,该处理程序处理并向轮询客户端发送通知。我每 10 秒使用 jquery 进行 ajax 调用,等待通知。问题是每次我将响应发送回客户端时,它总是调用错误回调。使用提琴手,我看到 json 响应以状态 200 到达客户端,但在萤火虫中,请求一直在等待。
客户:
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);
【问题讨论】:
-
尝试使用相对 URL,以防浏览器认为这里发生了某种 XSS:
url: "/NotificationChecker.ashx",