Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Not able to call WCF Service using jQuery

$
0
0

Hi,

I am trying to call WCF service using jquery as below

//IService.cs

        

public interface IService1
    {

        [OperationContract]
        string GetData();   

    }

//Service1.svc.cs

public string GetData()
        {
            SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=databasename;User ID=userid;Password=password");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from tablename", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();
            DataTable dt = ds.Tables[0];
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }  

Have written the below in Default.aspx

<script type="text/javascript" src="js/jquery-1.10.2.js"></script>  
    <script type="text/javascript">
        $(function () {
            debugger;
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: 'Service.svc/GetData',
                success: function (data) {
                    debugger;
                    //alert(data.d);
                    $($.parseJSON(data.d)).each(function (index, value) {                        
                        $("#TableID").append("<tr><td>" + value.MenuTitle + "</td><td>" + value.Description + "</td><td>"+ value.Url + "</td></tr>");
                    });
                },
                error: function (result) {
                    alert(result);
                }
            });

        });
        // });  
    </script>  

Post running Default.aspx,getting alert as object Object.

Please let me know if I am missing anything here.

Regards,

Sudheer 


Thanks & Regards, Sudheer


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>