1

from my client side i send json data to data.ashx file but i am not being able to read data from ProcessRequest method of ashx file. just can not understand why i am getting null

this way i am sending data from client side to ashx file

                var FeedCrd = {};
                FeedCrd["Name"] = $("input[id*='txtName']").val();
                FeedCrd["Subject"] = $("input[id*='txtSubject']").val();
                FeedCrd["Email"] = $("input[id*='txtFEmail']").val();
                FeedCrd["Details"] = $("textarea[id*='txtDetails']").val();


                $.ajax({
                    type: "POST",
                    url: urlToHandler + "?ac=send",
                    data: JSON.stringify(FeedCrd),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data == "SUCCESS");
                        {
            //
                        }

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }

                });

here is my ashx file code for ProcessRequest

 public void ProcessRequest(HttpContext context)
    {
        string outputToReturn = "";
        context.Response.ContentType = "text/html";

        if (context.Request.QueryString["ac"] == "send")
        {
                string sName = context.Request["Name"];
                string sSubject = context.Request["Subject"];

                outputToReturn = "SUCCESS";
        }
        context.Response.Write(outputToReturn);
    }

i have also seen how data is going to server side using firebig. here is the data {"Name":"cvv","Subject":"fdsfd","Email":"dsdsa@xx.com","Details":"wow"}

so please help me how to read data from ashx file when json send from client side. please tell me where i made mistake. please guide me. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • here i asked how to read json data from ProcessRequest() method. not client side. – Thomas Sep 25 '12 at 14:14
  • 1
    http://stackoverflow.com/questions/423294/best-way-to-parse-json-data-into-a-net-object, http://stackoverflow.com/questions/401756/parsing-json-using-json-net, http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – Andreas Sep 25 '12 at 14:32
  • thanks for hints but i was looking for solution to read json data from ashx file means asp.net httphandler. – Thomas Sep 25 '12 at 17:57
  • And where is the problem? http://blogs.interfacett.com/getting-data-asp-net-applications-using-ajax-jquery-using-custom-httphandler – Andreas Sep 25 '12 at 18:23

1 Answers1

4

First point to note is that make sure you always check for null or Empty strings in the context.Request Object

Next is your response should be a JSON Object but you are just returning a String.. Construct into JSON before sending from the .ashx handler

public void ProcessRequest(HttpContext context)
{
    string outputToReturn = String.Empty;   // Set it to Empty Instead of  ""
    context.Response.ContentType = "text/json";
    var ac = string.Empty ;
    var sName = String.Empty ;
    var sSubject = String.Empty ;

    // Make sure if the Particular Object is Empty or not
    // This will avoid errors
    if (!string.IsNullOrEmpty(context.Request["ac"]))
    {
        ac = context.Request["ac"];
    }


    if (ac.Equals("send")) // Use Equals instead of just =  as it also compares objects
    {
        if (!string.IsNullOrEmpty(context.Request["Name"]))
        {
            sName = context.Request["Name"];
        }
        if (!string.IsNullOrEmpty(context.Request["Subject"]))
        {
            sSubject = context.Request["Subject"];
        }
        // You need to Send your object as a JSON Object
        // You are just sending a sting 

        outputToReturn =  String.Format("{ \"msg\" : \"{0}\"  }", "SUCCESS" ) ;
    }
    context.Response.Write(outputToReturn);
}

// Your ajax should look like this in this case

$.ajax({
    type: "POST",
    url: urlToHandler + "?ac=send",
    data: JSON.stringify(FeedCrd),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        if( data != null){
            if (data.msg == "SUCCESS"); {

              alert( data.msg)
            }
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }

});​
Sushanth --
  • 55,259
  • 9
  • 66
  • 105