当我使用webservice获取数据

时间:2015-08-25 12:16:52

标签: jquery ajax web-services

我需要使用webservice从客户表中显示id,但我无法使用belove代码获取它。

在浏览器控制台中获取响应。 get json result

这是我获取或显示整个结果的ajax调用,但它无法获取客户ID



     $(document).ready(function () {
                var values;
                $.ajax({
                    url: 'http://localhost:53562/WebService.asmx/HelloWorld',                
                    type: 'POST',               
                    success: function (data) {
                        alert("ajax call");
                        //$.map(data, function (product) {
                        //    alert(product.Id);
                        //    $('<tr> <td>' + product.Name + '</td> <td>' + product.ProductNumber + ' </td> <td>' + product.SafetyStockLevel + ' </td> <td>' + product.ReorderPoint + ' </td></tr>').appendTo(".tblData");
                        //});
                        //for (var i in data) {
                        //    alert(data[i]);
                        //    // data[i].something, etc
                        //}
                        //var datas=data.par                   
                        $.each(data, function (index, item) {
                            alert(item);
                        });
                    }
                  
                });
            });
&#13;
&#13;
&#13;

This is my webservice code to get customer id from database and convert result to json and return this result to json call.
    [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            // getting connection string
            string conStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DbConection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(conStr))
            {
                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand("select Id from customer", conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                System.Web.Script.Serialization.JavaScriptSerializer serilizer = new System.Web.Script.Serialization.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 serilizer.Serialize(rows);   
            }      

2 个答案:

答案 0 :(得分:0)

来自MSDN文档:

Serialize(Object) : Converts an object to a JSON string.

这似乎是一个json字符串而不是一个有效的json,所以在这种情况下你必须在循环遍历响应中的每个对象之前解析它:

var resp = JSON.parse(data); // converts a json string to a valid json object
$.each(resp, function (index, item) { // use var resp in here 
    alert(item.id);
});

答案 1 :(得分:0)

我认为你应该指定你从ajax请求获得的数据类型,因为它似乎被读作字符串。 对于您的ajax请求选项,请尝试添加:

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