【问题标题】:No response from generic handler通用处理程序没有响应
【发布时间】:2013-06-16 03:58:08
【问题描述】:

我的 AjaxHandler.ashx 有问题,因为 context.Request["Action"] 在我使用 JQuery 调用发送时为空,有人可以帮我吗

注意:我使用的是html控制器而不是asp,net服务器控制器

<script type="text/javascript">

    $(function () {

        $("#btnSearch").click(function () {

            /*var skill = $("#ddlSkills option:selected").val();
            var types = $("#ddlTypes option:selected").val();
            var topics = $("#ddlTopics option:selected").val();
            var sortBy = $("#ddlSortBy option:selected").val();
            */
            $.ajax({
                url: "../ideapark/DesktopModules/ResourcesFilter/AjaxHandler.ashx",
                contentType: "application/json; charset=uft-8",
                type: "POST",
                data: $('form').serialize(),
                success: function(data) {
                    for(var i = 0; i < data.length; i++)
                    {
                        //t ajax handler recollection of resources.
                        //U NEED TDESERIALIZE 
                        var resID = data.response[i].ID;
                        var summary = data.response[i].Summary;
                        var pageID = data.response[i].PageID;
                        var name = data.response[i].Name;
                        var createdOn = data.response[i].CreatedOn
                        var Total = data.response[i].Total;
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown)
                {
                    alert(errorThrown);
                    alert(XMLHttpRequest);
                    alert(textStatus);
                    console.log(errorThrown);
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        });

    });
</script>
/// <summary>
    /// Summary description for AjaxHandler
    /// </summary>
    public class AjaxHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
           //context.Response.Write("Hello World");

           string response = "";
          string var =  context.Request["Action"].ToString();
           switch (context.Request["Action"])
           {
               case "ResponseFilterSearch":
                   response += "{";


                   Dictionary<string, Object> jsonObject = new Dictionary<string, object>();
                   string skill = context.Request["Skill"].ToString();
                   string type = context.Request["Type"].ToString();
                   string focus = context.Request["focus"].ToString();
                   string keyword = context.Request["Keyword"];
                   string sortby = context.Request["SortBy"];
                   string pageNumber = context.Request["pagenumber"];

                   SqlDataProvider sqlConn = new SqlDataProvider();

                   DataSet dsResults = SqlHelper.ExecuteDataset(sqlConn.ConnectionString, "spResourceSearch", skill, type, focus, keyword, sortby, pageNumber);

                   foreach (System.Data.DataRow row in dsResults.Tables[0].Rows)
                   {
                       response += "\"ID\":" + "\"" + row["Id"].ToString() + "\"";
                       response += "\"Summary\":" + "\"" + row["summary"].ToString() + "\"";
                       response += "\"PageID\":" + "\"" + row["pageId"].ToString() + "\"";
                       response += "\"Name\":" + "\"" + row["name"].ToString() + "\"";
                       response += "\"CreatedOn\":" + "\"" + row["createdOn"].ToString() + "\"";
                       response += "\"Total\":" + "\"" + row["total"].ToString() + "\"";

                   }



                   response += "}";

                   break;

           }

           //this returns a json string

           context.Response.Write(response);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

【问题讨论】:

    标签: c# jquery asp.net json


    【解决方案1】:

    您似乎混淆了contentType 和dataType。

    contentType 表示请求正文的内容类型(即您发送到服务器的数据),您已将其设置为 json,这不是 $('form').serialize() 将产生的。 $('form').serialize() 在 application/x-www-form-urlencoded 中生成数据,这是 $.ajax 中的默认值。

    现在dataType 是响应正文的内容类型(这是您从服务器接收的数据),您的代码应该是 json。

    $.ajax({
        url: "../ideapark/DesktopModules/ResourcesFilter/AjaxHandler.ashx",
        dataType: "json",
        type: "POST",
        data: $('form').serialize(),
        success: function(data) {
        ...
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
        ....
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 2014-04-22
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多