2012-05-03 34 views
0

我試圖發送一個由jqueryui對話框窗體上的客戶輸入的值給webhandler(ashx)使用 ajax post沒有成功,下面是代碼:使用json ajax從jqueryui對話框發送值到web處理程序[.ashx]

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> 

誰能幫助我如何傳遞的參數處理程序中,任何援助或建議將不勝感激,謝謝

回答

2

您需要在您的ashx喜歡用HttpContext.Current.Request

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

和更新您的Ajax調用是這樣的:

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 
     } 
+0

我試過,但是,我得到一個錯誤:錯誤非可調用成員'System.Web.HttpRequest.Params'不能像方法一樣使用。 –

+0

是的,我已經更新了它。現在應該工作。 –

+0

檢查了谷歌瀏覽器控制檯,得到:/login.ashx 500(內部服務器錯誤) –

0

我不能添加評論到當我們去Codrin Eugeniu時回覆;只是刪除

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

並再試一次。希望它能工作。

相關問題