【问题标题】:I unable to get json result when i use webservice to get data当我使用 web 服务获取数据时,我无法获得 json 结果
【发布时间】:2015-08-25 12:16:52
【问题描述】:

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

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

这是我获取或显示整个结果的 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);
                        });
                    }
                  
                });
            });
这是我从数据库中获取客户 ID 并将结果转换为 json 并将此结果返回给 json 调用的 Web 服务代码。
    [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);   
            }      

【问题讨论】:

  • 您能否在success 函数中添加一个console.log(data) 并发布您在控制台中获得的内容?
  • 请将响应粘贴为文本而不是图像,因为那些使用较小显示器(或在移动浏览器上)的人会发现很难阅读。
  • 由于您使用的是JavaScriptSerializer,我认为您可能会收到data.d 的响应数据,但只是想在发布答案之前进行确认。
  • palash 我在 console.log(data) 中得到了完美的结果,但是显示这个结果有问题
  • console.log(data); 是字符串还是对象?

标签: jquery ajax web-services


【解决方案1】:

来自 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);
});

【讨论】:

  • 根本不工作......甚至像 data.responceJSON 这样的解析也不工作。
  • success: function (data) { 你解析过这个datadata.responseJSON 因为根据你的回复img 没有名为responseJSON 的键。但事实是,如果您有一个成功的 ajax 响应,那么在成功函数中,arg 名称 data 就是您得到的实际响应。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2014-12-03
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多