Json数据没有很好地形成错误

时间:2016-05-06 06:32:56

标签: html angularjs vb.net web-services

json数据没有绑定到表

控制器代码

node whateverthename.js

网络服务代码

 $http(
            {
                method: 'post',
                url: 'Service.asmx/WPGetDS',
                data: $.param({ as_sql: "select    * from testtab", strConKey: "Etech" }),
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data, status, headers, config) {               
                var myjson = JSON.parse(data);
                $scope.dtDioSearch = myjson;
                console.log(myjson);
            }).error(function (data, status, headers, config) {
                console.log(data);
            });

HTML代码

Public Sub WPGetDS(ByVal as_sql As String, ByVal strConKey As String)
        Dim dt As New DataTable()
        Dim conGlobal As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(strConKey).ConnectionString)
        Dim a(0) As String
        Dim dr As DataRow
        Dim dtDataTable As DataTable
        If conGlobal.State = ConnectionState.Closed Then conGlobal.Open()
        Dim SDA = New SqlDataAdapter(as_sql, conGlobal)
        Dim DS As DataSet = New DataSet()
        Dim data As New WPData
        Dim js As New JavaScriptSerializer()
        Dim lCmdSql, lCmdErr As New SqlCommand
        Try


            dtDataTable = New DataTable("Table")
            Dim dcolSrNo As DataColumn

            dcolSrNo = New DataColumn("SlNo")
            dcolSrNo.AutoIncrement = True
            dcolSrNo.AutoIncrementSeed = 1
            dcolSrNo.AutoIncrementStep = 1
            dtDataTable.Columns.Add(dcolSrNo)

            DS.Tables.Add(dtDataTable)
            SDA.Fill(DS, ("Table"))
            SDA.Dispose()

            data.Message = ConvertDataTableTojSonString(DS.Tables(0))

            Context.Response.Write(js.Serialize(data.Message))
        Catch ex As Exception
            dt.Columns.Clear()
            dt.Columns.Add("Error")
            dr = dt.NewRow
            dr.Item("Error") = ex.Message.Trim
            dt.Rows.Add(dr)
            DS.Tables.Add(dt)
            conGlobal.Close()

            data.Message = ConvertDataTableTojSonString(DS.Tables(0))
            Context.Response.Write(js.Serialize(data.Message))
        Finally
            If conGlobal.State = ConnectionState.Open Then conGlobal.Close()
        End Try
    End Sub

控制台Json数据

<div class="table-responisive">
 <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>Test</th>        
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="erdata in dtDioSearch  track by $index">
        <td>{{erdata.SlNo}}</td>
        <td>{{erdata.Test}}</td>            
      </tr>
    </tbody>
  </table>
  </div>

我的问题是json数据没有绑定到html表。在Firefox中有一个错误显示在控制台中格式不正确。请帮忙......

2 个答案:

答案 0 :(得分:1)

成功回调的第一个参数是一个包含许多属性的JavaScript对象,包括一个data属性,其值是基于API返回的JSON的已解析JavaScript对象。尝试解析JavaScript对象将导致错误。

尝试将成功方法修改为:

.success(function (response, status, headers, config) {               
   var myjson = response.data;
   $scope.dtDioSearch = myjson;
});

答案 1 :(得分:0)

Public Function GetJSon(ByVal dt As DataTable) As List(Of Dictionary(Of String, Object))
        Dim rows As New List(Of Dictionary(Of String, Object))
        Dim row As Dictionary(Of String, Object)

        'Return JsonConvert.SerializeObject(dt).ToList
        'Return JSONString

        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)
            For Each col As DataColumn In dt.Columns
                If col.DataType = GetType(Date) Then
                    Dim dtt As DateTime = DateTime.Parse(dr(col).ToString())
                    row.Add(col.ColumnName, dtt.ToString("dd-MM-yyyy hh:mm:ss"))
                Else
                    row.Add(col.ColumnName, dr(col))
                End If

            Next
            rows.Add(row)
        Next
        Return rows
    End Function

@tj感谢您的支持。问题是json返回字符串,我将其更改为数组列表