【问题标题】:AJAX json return causing Uncaught SyntaxError: Unexpected tokenAJAX json 返回导致 Uncaught SyntaxError: Unexpected token
【发布时间】:2015-12-14 18:25:22
【问题描述】:

大家好,我不知道为什么会出现以下错误:

Uncaught SyntaxError: Unexpected token

从 AJAX 调用中获取我的 JSON 字符串时。

我的 AJAX 代码是:

$.ajax({
   type: "POST",
   crossDomain: true,
   dataType: 'json',
   cache: false,
   contentType: "application/json",
   url: serviceURL,
   data: JSON.stringify({ febData: ["LIST"] }),
   success: function (data) {
         var obj = $.parseJSON(data);
         //console.log(data);             
      },
   error: function (xhr, status, error) {
      console.log(xhr.responseText);
   }
});  

错误就行了:

$.parseJSON(data)

我返回的 JSON 如下所示(使用 console.log(data) 时):

[{
        "RowTitle" : "1 A Test",
        "FormName" : "F_Form1",
        "ID" : "9d91523e-5e76-47bf-860d-3bbb41933b74"
    }, {
        "RowTitle" : "1 A Test",
        "FormName" : "F_Form1",
        "ID" : "9d91523e-5e76-47bf-860d-3bbb41933b74"
    }, {
        "RowTitle" : "1 Diagnostic",
        "FormName" : "F_Form1",
        "ID" : "a97847c9-acf3-4719-8109-91bf3772c2ba"
    }, {
        "RowTitle" : "1 poll - copy",
        "FormName" : "F_Form1",
        "ID" : "435cbb7b-c3dd-43f4-84aa-61a343d6abe9"
    }, {
        "RowTitle" : "1 poll - copy",
        "FormName" : "F_Form1",
        "ID" : "435cbb7b-c3dd-43f4-84aa-61a343d6abe9"
    }, {
        "RowTitle" : "1 poll idea",
        "FormName" : "F_Form1",
        "ID" : "2fd81b31-d648-4e40-8019-a7967bb190de"
    }, {
        "RowTitle" : "1 poll idea",
        "FormName" : "F_Form1",
        "ID" : "2fd81b31-d648-4e40-8019-a7967bb190de"
    }, {
        "RowTitle" : "2015 Family Holiday Gathering",
        "FormName" : "F_Form1",
        "ID" : "9ae2d44f-8e65-465a-8802-e2ad4408afd2"
    }
]

我正在通过我的 .net 网络服务返回该 JSON:

Dim jsonResults As String = "["

For value As Integer = 0 To result_RowTitle.Length - 1
   jsonResults += "{""RowTitle"":""" & result_RowTitle(value) & ""","
   jsonResults += """FormName"":""" & result_rowFormName(value) & ""","
   jsonResults += """ID"":""" & result_RowIDs(value) & """},"
Next

jsonResults = jsonResults.Trim().Substring(0, jsonResults.Length - 1) & "]"

Dim strResponse As String = ser.Serialize(jsonResults)

Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Flush()
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()

有人知道它可能在哪里搞砸导致该错误吗?

网络截图要求

按照建议创建

Private Class febForms
    Public Property RowTitle As String
    Public Property FormName As String
    Public Property ID As String
End Class

Dim febFormData As New List(Of febForms)

For value As Integer = 0 To result_RowTitle.Length - 1
   Dim febforms As New febForms

   With febforms
      .RowTitle = result_RowTitle(value)
      .FormName = result_rowFormName(value)
      .ID = result_RowIDs(value)
   End With

   febFormData.Add(febforms)
Next

Dim strResponse As String = ser.Serialize(febFormData)

Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Flush()
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()

【问题讨论】:

  • 不要手动构建 JSON。使用 JSON 序列化程序来序列化类。
  • @SLaks 因为我有 3 种类型的返回数据,所以有一个例子来说明这一点吗?
  • 使用这些属性创建一个类。
  • 可能strResponse.Length.ToString() 没有得到正确的大小,因为content-length 必须以字节为单位。
  • Json 验证器 jsonlint.com 说你的回答是正确的(当然永远不要手工制作 json),那么,你的问题是什么?我认为错误在于您正在尝试解析已经解码的 json,jQuery 会为您做到这一点......所以,试试var obj = data;

标签: jquery json ajax vb.net web-services


【解决方案1】:

.Net 字符串是多字节的,因此字符串长度与发送到流的内容长度不同。这可能意味着您在客户端上的解析没有处理完整的 JSON,因为字符数小于字节数意味着它被修剪了。

服务器上这样的东西可能会有所帮助:

Encoding encoding = HttpContext.Current.Response.ContentEncoding;
byte[] buffer = encoding.GetBytes(strResponse);
Context.Response.OutputStream.Write(buffer,0,buffer.Length);

您的标头大小值应为buffer.length

【讨论】:

    【解决方案2】:

    不要手动构建 JSON。使用 JSON 序列化器对类进行序列化,以确保您的 JSON 确实有效。

    您还需要将Content-Length 设置为UTF8 字节的长度。使用Encoding.UTF8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      相关资源
      最近更新 更多