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.
- public class Feedback : IHttpHandler
- {
-
- public void ProcessRequest(HttpContext context)
- {
- string machine_ip = "", remote_ip = "";
- DataTable dt = new DataTable();
- string outputToReturn = "";
- context.Response.ContentType = "text/html";
- string strCountryCode = CountryCookie.GetCookieValue();
- strCountryCode = (strCountryCode.Trim() == "" ? "GBR" : strCountryCode);
-
- machine_ip = Utility.GetMachineIP();
- remote_ip =Utility.GetRemoteIP();
-
- if (context.Request.QueryString["ac"] != "" && context.Request.QueryString["ac"] != null)
- {
- if (context.Request.QueryString["ac"] == "send")
- {
- var jss = new JavaScriptSerializer();
- string json = new StreamReader(context.Request.InputStream).ReadToEnd();
- Dictionary<string, string> sData = jss.Deserialize<Dictionary<string, string>>(json);
- string _Name = sData["Name"].ToString();
- string _Subject = sData["Subject"].ToString();
- string _Email = sData["Email"].ToString();
- string _Phone = sData["Phone"].ToString();
- string _Details = sData["Details"].ToString();
- string _url = sData["url"].ToString();
- string _service = sData["Service"].ToString();
- string _website = sData["Website"].ToString();
-
- SendMail(_Name, _Subject, _Email, _Details, context.Request.QueryString["CountryCode"], "", "", _Phone, _url);
- outputToReturn = "SUCCESS";
- }
-
- }
- context.Response.Write(outputToReturn);
- }
-
- private string SendMail(string Name, string Subject, string Email, string Details, string strCountry, string partname, string parttype, string phone, string _url)
- {
-
- }
-
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
JavaScript
-
-
- var urlToHandler = location.protocol + "//" + location.host + "/Feedback.ashx";
-
- var FeedCrd = {};
- FeedCrd["Name"] = $("input[id*='txtName']").val();
- FeedCrd["Subject"] = $("input[id*='txtSubject']").val();
- FeedCrd["Email"] = $("input[id*='txtFEmail']").val();
- FeedCrd["Phone"] = $("input[id*='txtfphone']").val();
- FeedCrd["Details"] = $("textarea[id*='txtDetails']").val();
- FeedCrd["url"] = window.location.href;
- FeedCrd["Service"] = $("[id*='ddlService']:visible option:selected").text();
- FeedCrd["Website"] = $("[id*='ddlWebsite']:visible option:selected").text();
-
-
- $.ajax({
- type: "POST",
- url: urlToHandler + "?ac=send&CountryCode=" + feedCookie,
- data: JSON.stringify(FeedCrd),
- success: function(data) {
-
- if (data == "SUCCESS") {
- setTimeout(function() {
-
- if (_AdjustHeight == 0) {
- $(".ui-dialog").animate({
- height: '+=' + dialog_loader.height + 'px'
- }, 600, function() {
- _AdjustHeight = 1;
-
-
-
- $('#feed_loader').html('<span>' + dialog_Msg.Success + '</span>');
- });
- } else {
- $('#feed_loader').fadeOut('slow', function() {
- $('#feed_loader').html('<span>' + dialog_Msg.Success + '</span>');
- }).fadeIn('slow');
- }
-
- setTimeout(function() {
- close();
- }, 2000);
-
- }, 2000);
-
- }
- },
- error: function(XMLHttpRequest, textStatus, errorThrown) {
- alert(textStatus);
- }
-
- });