Error - Error: Uncaught SyntaxError: Unexpected token :
平台-Icenium。
我们正在使用远程服务 - http://localhost:35798/RestServiceImpl.svc/json
来获取数据。我附上了从服务收到的数据的格式。
这是我的代码:
var dataSource = new kendo.data.DataSource({
schema: {
data: "d"
},
transport: {
read: {
url: "http://localhost:35798/RestServiceImpl.svc/json",
dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
data: {
id: "4"
},
type: "GET",
contentType: "application/json;charset=utf-8"
},
change: function () {
alert('called');
var data = this.data();
console.log(data.length); // displays "77"
debugger;
$('#txtJson').val(data[0].name);
}
}
});
$("#submitButton").click(function () {
dataSource.read();
var data = dataSource.data();
console.log(data.length);
});
这是我的服务代码 -
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json?id={id}")
]
List<Person> JSONData(string id);
问 1) 如何解决此错误 - Uncaught SyntaxError: Unexpected token
,我是否遗漏了什么?
Q 2) 单击按钮后,我调用 dataSource.read()
,之后 dataSource.data().length
变为 0。我认为这是应该在 dataSource.bind(change:function())
中处理。但是,在 dataSource.read()
之后,更改函数不会触发。
最佳答案
一旦您解决了 JSON 与 JSONP 的问题,数据仍然是空的,因为您没有在 datasource.schema
中说数据实际上位于接收到的名为 的 JSON 元素内JSONDataResult
。
架构
应该是:
schema: {
data: "JSONDataResult"
},
您可以将解析函数添加到架构中以调试您得到的内容:
schema : {
parse: function(response) {
console.log("parse");
console.log(JSON.stringify(response, null, 4));
debugger;
return response.JSONDataResult;
}
}
关于jquery - Kendo UI - 错误 : Uncaught SyntaxError: Unexpected token :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652364/