Monday, April 2, 2012

AJAX call to post/ get data

Post data to handler


function PostData() {

             if ($("#txtComment").val() != "") {
                 $("#spMessage").text("Please wait...");
                 var postData = {comments: $("#txtComment").val(),photoId: pid };
               
                $.ajax({

                    type: 'POST',
                  //  dataType: 'json',
                    url: "Handler/handerlername.ashx",
                    // contentType: 'application/json',
                    cache: false,
                    data: postData,
                    success: OnComplete,
                    error: OnFail
                });
                return false;
            }
        }


 function OnComplete() {
          // Load comment in a div
            $("#comments").load("Handler/HanderName.ashx?pid=" + pid+"&random=" + Math.random() * 99999);
     
        }

        function OnFail() {

            $("#spMessage").text("Error in Processing");
        }


// Handler Code to fetch Data

 public void ProcessRequest(HttpContext context)
        {
            
            int photoId = 0;
            int totalRows = 0;
            StringBuilder sb= new StringBuilder();
            photoId = clsCommon.ParseInt(context.Request["pid"]);
           

            DataTable table = GetDataTable()// Data from database
            if (table != null && table.Rows.Count>0)
            {

                foreach (DataRow dr in table.Rows)
                {
                  // Append html
                }

            }
            context.Response.Write(sb.ToString());
            
        }

// Handler Code to post data
  public void ProcessRequest(HttpContext context)
        {
          
           string Comments = string.Empty;
           int PhotoId = 0;
           int Memberid = 0;
            if (context.Request.RequestType == "POST")
            {
                // Logic to insert data in database
                if (result)
                {
                    context.Response.Write("Success!");
                }
                else
                {
                    context.Response.Write("Error in Processsing!");
                }


                
            }
            else
            {
                context.Response.Write("Data was not sent properly");
            }
        }

No comments:

Post a Comment