嘿所有我不知道为什么我会收到以下错误:
未捕获的SyntaxError:意外的令牌
从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(数据)时):
[{
"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()
答案 0 :(得分:0)
服务器上的这样的东西可能会有所帮助:
Encoding encoding = HttpContext.Current.Response.ContentEncoding;
byte[] buffer = encoding.GetBytes(strResponse);
Context.Response.OutputStream.Write(buffer,0,buffer.Length);
您的标题大小值应为buffer.length
。
答案 1 :(得分:0)
不要手工构建JSON。使用JSON序列化程序来序列化类,以确保您的JSON实际上有效。
您还需要将for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
arrayShifted[row][col] = array[row][(col + 1) % array[row].length]
}
}
设置为UTF8字节的长度。使用Content-Length
。