[英]Error during reading json in Aurelia.io example
我通过添加自定义模块来扩展aurelia的入门应用程序 。 读取json时出现错误“未捕获的SyntaxError:意外的令牌:”。 但是json验证程序没有发现任何错误。
这是我的json { "news": [ { "title": "Lorem Ipsum is simply dummy text", "type": "news", "tags": [ "news", "fish", "loremipsumdolor" ], "text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry." }, { "title": "Lorem Ipsum", "type": "news", "tags": [ "news", "fish" ], "text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s." } ] }
{ "news": [ { "title": "Lorem Ipsum is simply dummy text", "type": "news", "tags": [ "news", "fish", "loremipsumdolor" ], "text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry." }, { "title": "Lorem Ipsum", "type": "news", "tags": [ "news", "fish" ], "text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s." } ] }
这是我尝试加载json的模块(基本上是flickr.js的复制粘贴,可以在示例代码中找到)`import'inject} from'aurelia-framework'; 从'aurelia-http-client'导入{HttpClient};
@inject(HttpClient)
export class News{
heading = 'News';
items = [];
url = '/res/data.json';
constructor(http){
this.http = http;
}
activate(){
debugger;
return this.http.jsonp(this.url).then(response => {
this.items = response.content.news;
});
}
canDeactivate(){
return confirm('Are you sure you want to leave?');
}
}
'当执行示例代码时,它将从flikr加载json并将其包装在jsonp_callback_92661()中。 那是唯一的区别。 这可能是我问题的根源吗?
是的,这就是问题的根源。 当您实际上不需要jsonp
时,便会使用它。 由于您自己提供的是静态json文件,并且大概是在主应用程序所在的同一域中提供的,因此您无需使用jsonp。 如果您还不熟悉,那么可以在SO中找到有关jsonp的很好的解释 (闪烁将响应包装在函数调用中,因为jsonp起作用是必需的)。
您应该可以使用get
:
return this.http.get(this.url).then(response => {
//not sure if response.content is an object already.
//you may need to JSON.parse response.content
this.items = response.content.news;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.