繁体   English   中英

jQuery ASP.NET处理程序的Ajax调用始终返回错误

[英]JQuery ajax call to ASP.NET handler always returns error

我正在为asp.net处理程序编写一个客户端,该处理程序处理并将通知发送到轮询客户端。 我每隔10秒钟用jquery进行一次ajax调用,以等待通知。 问题是每次我将响应发送回客户端时,它总是调用错误回调。 使用小提琴手,我看到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);

我实际上发现了问题。 问题是我从不在Web服务器上托管的本地计算机启动了客户端html,这引起了问题。 将其添加到本地Web服务器后,原始代码按原样工作。

我认为您的Ajax通话缺少以下内容:

    dataType: 'json',
    contentType: "application/json; charset=uft-8",

尝试这个:

$.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 });
}

我认为您的问题是您通过了这样的用户名

data: { userName: "ggyimesi" }

我想你应该这样

data: '{userName:"' + ggyimesi + '"}'

您可以添加更多这样的内容

 type: "POST",
 url: "NotificationChecker.ashx",
 contentType: "application/json; charset=utf-8",
 dataType: 'json',

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM