Mr A
Mr A

Reputation: 6778

Sending values to web handler [.ashx] from jqueryui dialog box using json ajax

I am trying to send a values entered by customer on jqueryui dialog form to webhandler (ashx) using ajax post with no success, below is the code:

Webhandler[login.ashx]

public class Login : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        // string ID = context.Request.QueryString["ID"];  
        //Want to pass full name , email and selected value of dropdown list 
        // then perform sql query
        string fullname = "";//from ajax
        string email = "";//from ajax
        string scheme = "";//from ajax
        docalculation(fullname, email , scheme);
}

    public bool IsReusable {
        get {
            return false;
        }
    }

            public void docalculation(string name, string email, string scheme)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["umbracoDbDSN"]);
                conn.Open();
                SqlCommand cmd1 = conn.CreateCommand();
                cmd1.CommandText = "IF NOT EXISTS (SELECT * FROM XXX WHERE email = @XX) INSERT INTO dbo.schemeCustomers(CustomerName, CustomerEmail, schemeType, LoginDate) VALUES (@XX,@XX,@XX,@XX)";
                cmd1.Parameters.Add("@XX", SqlDbType.VarChar).Value = email;
                cmd1.Parameters.Add("@XX", SqlDbType.VarChar).Value = scheme;
                cmd1.Parameters.Add("@XX", SqlDbType.DateTime).Value = DateTime.Now;
                int result1 = Convert.ToInt32(cmd1.ExecuteScalar());
                conn.Close();
                Response.Redirect("/home/schemes.aspx");// response.redirect doesnt works in handler??
            }

}

.aspx

$('#logintest').click(function (e) {
                    e.preventDefault();
                    var selectedValue = $('#DropDownList2').val();var fullname = $('#name').val();var email = $('#email').val();
                    var jsonData = '{ "fullname":" + fullname  +", "email": " + email +" , "selectedValue" : " + selectedValue +"}';
                    $.ajax({
                       url: "/login.ashx",

                        type: "POST",
                        data : JSON.stringify({ fullData: jsonData }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            if (response.d != "") {
                                alert(response.d);

                            }
                        }


                    });
                });

                 <div id="dialog-form" title="Group Scheme Login">
                <form>
                <fieldset>
                <label for="name">
                    Full Name</label>
                <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
                <br />
                <label for="email">
                    Email</label>
                <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
                <br />
                <select id="DropDownList2">
                    <option value="product1">Camera</option>
                    <option value="product2">DVD</option>
                    <option value="product3">AC</option>
                </select>
                </fieldset>
                </form>
                <br />
                <a href="" id="logintest">Login </a>
                </div>

Can anyone assist me on how to pass the parameters to handler, any assistance or suggestion will be highly appreciated , Thanks

Upvotes: 0

Views: 4910

Answers (2)

user1788956
user1788956

Reputation:

i cannot add comment into replies as we go on Codrin Eugeniu; just remove

 contentType: "application/json; charset=utf-8",

and try again. hope it'll work.

Upvotes: 0

Codrin Eugeniu
Codrin Eugeniu

Reputation: 1383

You would need to use HttpContext.Current.Request in your ashx like:

 string fullname =  HttpContext.Current.Request.Form("fullname");
 ...

and update your ajax call like this:

 var selectedValue = $('#DropDownList2').val(),
     fullname = $('#name').val(),
     email = $('#email').val();

$.ajax({
         url: "/login.ashx",
         type: "POST",
         data : { fullname: fullname, 
                  email:email, 
                  selectedValue:selectedValue },
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (response) { 
               // do stuff
         }

Upvotes: 2

Related Questions