【发布时间】: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