user1800361
user1800361

Reputation:

No response from generic handler

I have problem in my AjaxHandler.ashx becouse the context.Request["Action"] is null when I send it using JQuery call can someone help me

Note: I'm using html controller not asp,net server controller

<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;
            }
        }
    }

Upvotes: 0

Views: 1398

Answers (1)

Musa
Musa

Reputation: 97717

You seem to be mixing up contentType and dataType.

contentType represents the the content type of the request body(that is the data you're sending to the server), which you've set to json which is not what $('form').serialize() will produce. $('form').serialize() produces data in application/x-www-form-urlencoded which is the default in $.ajax.

Now dataType is the content type of the response body(which is the data you receive from the server) which from your code should be json.

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

Upvotes: 4

Related Questions