SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符

时间:2014-08-31 13:34:08

标签: javascript jquery ajax json

所以我试图通过Jquery Ajax调用返回一个DataTable到webservice但是仍然得到一个错误,即返回的JSON格式不正确。当我把字符串传递给验证器时,它说它很好,任何人都知道我在哪里出错? 我的代码如下:

 var params = new Object();
        params.centreId = 0;
        params.brcWeek = 0;
        params.brcMonth = 0;
        params.brcYear = 0;
        params.weekOffSet = 0;

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Webservice/LloydsService/getLloydsOverview",
            data: JSON.stringify(params),
            dataType: "json",
            success: function (data) {
                console.debug("data received Ok?");
                console.log(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + " -- " + "---" + errorThrown);
            }
        });

网络服务:

 <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function getLloydsOverview(ByVal centreId As Integer, ByVal brcWeek As Integer, ByVal brcMonth As Integer, ByVal brcYear As Integer, ByVal weekOffSet As Integer) As String
    Dim dt As New DataTable()
    Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()

    Using conn As SqlClient.SqlConnection = GetDBConnection()
        Using cmd As SqlClient.SqlCommand = conn.CreateCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "sp_LloydsDashboard_Overview"
            cmd.Parameters.AddWithValue("@CentreId", centreId)
            cmd.Parameters.AddWithValue("@BRCWeek", brcWeek)
            cmd.Parameters.AddWithValue("@BRCMonth", brcMonth)
            cmd.Parameters.AddWithValue("@BRCYear", brcYear)
            cmd.Parameters.AddWithValue("@WeekOffSet", weekOffSet)
            Using DA As New SqlDataAdapter(cmd)
                conn.Open()
                DA.Fill(dt)
                Dim rows As New List(Of Dictionary(Of String, Object))()
                Dim row As Dictionary(Of String, Object)
                For Each dr As DataRow In dt.Rows
                    row = New Dictionary(Of String, Object)()
                    For Each col As DataColumn In dt.Columns
                        row.Add(col.ColumnName, dr(col))
                    Next
                    rows.Add(row)
                Next
                Dim json As String = serializer.Serialize(rows)
                Return json
            End Using
        End Using
    End Using
End Function

返回此(验证):

[{"WalkedPast":1000,"PeelOff":0.1,"WalkedIn":100}]

但我仍然收到错误SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符。

任何想法?

1 个答案:

答案 0 :(得分:-1)

不要字符串化(假设params已经是json)

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Webservice/LloydsService/getLloydsOverview",
            data: params,
            dataType: "json",
            success: function (data) {
                console.debug("data received Ok?");
                console.log(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + " -- " + "---" + errorThrown);
            }
        });

小提琴here,查看控制台。