Send and Parse JSON Data from ASHX File

I like to share a small knowledge like how to send JSON data by JQuery ajax to ashx file and how to parse that JSON data from ashx file and return response to calling environment.

Here is my code. one part is related to JQuery and second part is asp.net server side code for ashx file.

ASP.Net code ASHX related.

  1. public class Feedback : IHttpHandler    
  2. {    
  3.   
  4.     public void ProcessRequest(HttpContext context)    
  5.     {    
  6.         string machine_ip = "", remote_ip = "";    
  7.         DataTable dt = new DataTable();    
  8.         string outputToReturn = "";    
  9.         context.Response.ContentType = "text/html";    
  10.         string strCountryCode = CountryCookie.GetCookieValue();    
  11.         strCountryCode = (strCountryCode.Trim() == "" ? "GBR" : strCountryCode);    
  12.   
  13.         machine_ip = Utility.GetMachineIP();    
  14.         remote_ip =Utility.GetRemoteIP();    
  15.   
  16.         if (context.Request.QueryString["ac"] != "" && context.Request.QueryString["ac"] != null)    
  17.         {    
  18.             if (context.Request.QueryString["ac"] == "send")    
  19.             {    
  20.                 var jss = new JavaScriptSerializer();    
  21.                 string json = new StreamReader(context.Request.InputStream).ReadToEnd();    
  22.                 Dictionary<stringstring> sData = jss.Deserialize<Dictionary<stringstring>>(json);    
  23.                 string _Name = sData["Name"].ToString();    
  24.                 string _Subject = sData["Subject"].ToString();    
  25.                 string _Email = sData["Email"].ToString();    
  26.                 string _Phone = sData["Phone"].ToString();    
  27.                 string _Details = sData["Details"].ToString();    
  28.                 string _url = sData["url"].ToString();    
  29.                 string _service = sData["Service"].ToString();    
  30.                 string _website = sData["Website"].ToString();    
  31.   
  32.                 SendMail(_Name, _Subject, _Email, _Details, context.Request.QueryString["CountryCode"], """", _Phone, _url);    
  33.                 outputToReturn = "SUCCESS";    
  34.             }    
  35.   
  36.         }    
  37.         context.Response.Write(outputToReturn);    
  38.     }    
  39.   
  40.     private string SendMail(string Name, string Subject, string Email, string Details, string strCountry, string partname, string parttype, string phone, string _url)    
  41.     {    
  42.     // send mail code    
  43.     }    
  44.   
  45.     public bool IsReusable    
  46.     {    
  47.         get    
  48.         {    
  49.             return false;    
  50.         }    
  51.     }    
  52. }   

JavaScript

  1. // from here i am making call to ashx and sending json data there  
  2. // any one can see how json data is sending to ashx file   
  3. var urlToHandler = location.protocol + "//" + location.host + "/Feedback.ashx";  
  4.   
  5. var FeedCrd = {};  
  6. FeedCrd["Name"] = $("input[id*='txtName']").val();  
  7. FeedCrd["Subject"] = $("input[id*='txtSubject']").val();  
  8. FeedCrd["Email"] = $("input[id*='txtFEmail']").val();  
  9. FeedCrd["Phone"] = $("input[id*='txtfphone']").val();  
  10. FeedCrd["Details"] = $("textarea[id*='txtDetails']").val();  
  11. FeedCrd["url"] = window.location.href;  
  12. FeedCrd["Service"] = $("[id*='ddlService']:visible option:selected").text();  
  13. FeedCrd["Website"] = $("[id*='ddlWebsite']:visible option:selected").text();  
  14.   
  15.   
  16. $.ajax({  
  17.     type: "POST",  
  18.     url: urlToHandler + "?ac=send&CountryCode=" + feedCookie,  
  19.     data: JSON.stringify(FeedCrd),  
  20.     success: function(data) {  
  21.   
  22.         if (data == "SUCCESS") {  
  23.             setTimeout(function() {  
  24.   
  25.                 if (_AdjustHeight == 0) {  
  26.                     $(".ui-dialog").animate({  
  27.                         height: '+=' + dialog_loader.height + 'px'  
  28.                     }, 600, function() {  
  29.                         _AdjustHeight = 1;  
  30.                         //                                        $('#feed_loader').fadeOut('slow', function () {    
  31.                         //                                            $('#feed_loader').html('<span>' + dialog_Msg.Success + '</span>');    
  32.                         //                                        }).fadeIn('slow');    
  33.                         $('#feed_loader').html('<span>' + dialog_Msg.Success + '</span>');  
  34.                     });  
  35.                 } else {  
  36.                     $('#feed_loader').fadeOut('slow', function() {  
  37.                         $('#feed_loader').html('<span>' + dialog_Msg.Success + '</span>');  
  38.                     }).fadeIn('slow');  
  39.                 }  
  40.   
  41.                 setTimeout(function() {  
  42.                     close();  
  43.                 }, 2000);  
  44.   
  45.             }, 2000);  
  46.   
  47.         }  
  48.     },  
  49.     error: function(XMLHttpRequest, textStatus, errorThrown) {  
  50.         alert(textStatus);  
  51.     }  
  52.   
  53. });