Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me ..
I m creating a json object by jquery , but it is not handled by httpcontext in handler page.

.aspx page
------------------
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript">
</script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(function () {
            var jsonobj = [];
            var jsonStr = "";
            $("#btngen").click(function () {

                $("#Content").empty();
                var txt = "<input type='Text'/><br />";
                var num = $("#txt_numnber").val();
                $("#Content").append("<table>");
                for (var i = 0; i < num; i++) {

                 var tbl = "<tr><td>Cheque Number</td><td><input type='text' id='txtChq" + i + "'/></td><td>Amount</td><td><input type='text' id='txtAmount" + i + "'/></td><table><tbody><tr>";

                    $("#Content").append(tbl);
                }
                $("#Content").append("</tr></tbody></table></tr></table>");
            });

            /*#btnget*/
            $("#btnget").click(function () {
                var i = 0;
                var urltohandle = 'ChecqueDetails.ashx';

                var chk = $("#Content").children();

                ////                var jsonobj = [];
                ////                var jsonStr = "";
                ////                var ConstStr = ",";

                var final;
                if ($(chk).length > 0) {
                    for (var k = 0; k < ($(chk).length - 1) / 2; k++) {
                        var ChkValue = $("#txtChq" + k).val();
                        var Chkamt = $("#txtAmount" + k).val();
                        // alert(ChkValue + "  " + Chkamt);
                        jsonobj.push({ "ChequeNumber": ChkValue, "Amount": Chkamt });


                    }

                    jsonStr = JSON.stringify(jsonobj);

                    jsonStr = jsonStr.replace('[', '');
                    jsonStr = jsonStr.replace(']', '');
                    ////                    alert(jsonStr);

                    ////                    var _obj = jQuery.parseJSON(jsonStr);

                    ////                    alert(_obj[0].ChequeNumber);

                    ////                    jsonStr = JSON.stringify(_obj);

                    ////                    alert(jsonStr);

                    $("#<%= hidden_value.ClientID %>").text(jsonStr);
                    $("#<%= lbl_json.ClientID %>").text(jsonStr);

                                        $.ajax({
                                                    url: urltohandle,
                                                 //   type:"POST",
                                                    cache: true,
                                                    async: true,
                                                    dataType: "json",
                                                    contentType: "application/json; charset=utf-8",
                                                    data: jsonStr,
                                                    success: function (data) {
                                                    alert(data);
                                                    },
                                                    error: function (data, status, jqXHR) {
                                                    alert('There was an error.');
                                                    }

                                        }); 



                    ////                    console.log(jsonStr);
                    ////                    var _obj = jQuery.parseJSON(jsonStr);
                    ////                    $("#lit_txt").text(jsonStr);
                    ////                    console.log(_obj[0].chk);
                }
            });

            $("#<%=btnColor.ClientID %>").click(function () {
            
            });

});
    
    </script>
 
</head>
<body>
    <form id="form1" runat="server" action="Default4.aspx" method="post">
    <input type="button" id="btngen" value="Generate" />
    <input type="text"  id="txt_numnber" />
    <div id="Content">
    
    </div>
    <input type="hidden" runat="server" id="hidValue"/>

   
    <input type="button" runat="server" id="btnget" value="Get" />
    <div id="txt_lit">
    </div>
    <asp:Label runat = "server" ID="lbl_json" EnableViewState="true">
    <asp:HiddenField runat="server" ID="hidden_value" />
    
    <asp:Button runat="server" ID="btnColor" Text="Change" 
        onclick="btnColor_Click" />
    </form>
   
</body>
</html>

=================
.ashx file
=================
<%@ WebHandler Language="C#" Class="ChecqueDetails" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Runtime.Serialization;

[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
public class ChecqueDetails : IHttpHandler
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
       
       object obj=context.Request;
      
       // JavaScriptSerializer serializer = new JavaScriptSerializer();
       // string str = serializer.Serialize(obj);        
        
       // System.Web.Script.Serialization.JavaScriptSerializer jsp = (System.Web.Script.Serialization.JavaScriptSerializer)context;


        string ChequeNumber = context.Request["ChequeNumber"];
        
        
  }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

can anybody help me ? please ....
Thanks in advance
Posted
Updated 6-Sep-12 23:45pm
v2
Comments
Dibu.net 7-Sep-12 4:34am    
the ChequeNumber is still null , although the url has the json data .. please help me
Andrei Straut 7-Sep-12 4:35am    
Please update your question first, and highlight your code. Use the 'code' tag immediately above the textarea where you pasted your question
Dibu.net 7-Sep-12 4:41am    
I have images for details but how to attach with question ?
Andrei Straut 7-Sep-12 5:06am    
No, not images.
Edit your question, and:
in the sections where you have pasted code, simply add <pre> before your code, and </pre> after your code
Dibu.net 7-Sep-12 5:42am    
ok let me try to edit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900