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

Javascript SyntaxError:JSON.parse:JSON数据第3行第1列处的意外字符,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,因此,我试图通过jqueryajax调用返回一个DataTable到一个webservice,但是不断得到一个错误,返回的JSON格式不正确。当我将字符串传递给验证器时,它会说没问题,有人知道我的错误在哪里吗? 我的代码如下: var params = new Object(); params.centreId = 0; params.brcWeek = 0; params.brcMonth = 0; params.brcYear

因此,我试图通过jqueryajax调用返回一个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
但我仍然在JSON数据的第3行第1列得到了错误SyntaxError:JSON.parse:unexpected字符

有人有什么想法吗?

不要字符串化假定参数已经是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);
            }
        });

拨弄一下,看看控制台。

将web服务抽象掉。这不相关。到底是什么给了你语法错误?web服务或JavaScript处理响应听起来像是后者,但清楚永远不会有什么坏处?处理JSON的哪一行代码抛出了错误?ajax调用的响应会直接返回到error-errorhorn,在JSON数据的第3行第1列显示带有错误抛出测试SyntaxError:JSON.parse:unexpected字符的警报框。您如何检查webservice返回的内容?您是否正在检查开发人员工具的Net选项卡以查看对实际ajax请求的响应?使用firefox开发工具,检查console和response i get custom error page,但状态代码是302。因此决定在警报框中输出错误。params不是JSON。它是一个JavaScript对象。如果OP遵循您的建议,那么jQuery将自动对其进行编码,但将其作为表单数据,而不是JSON!。。。。因此jQuery在内部对其进行了字符串化。否则,它会在json中发布一个加密的字符串,从而导致错误。感谢您的否决(w/o Approval)发送表单数据而不是JSON如何解决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);
            }
        });