edt*_*ead 2 javascript json jsonp aurelia
我正在通过添加自定义模块来扩展aurelia的入门应用程序.我在阅读json时遇到错误"Uncaught SyntaxError:Unexpected token:".但是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."
}
]
}
这是我的模块试图加载json(基本上它是来自flickr.js的复制粘贴,可以在示例代码中找到)`import {inject}来自'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?');
}
}
Run Code Online (Sandbox Code Playgroud)
'当执行示例代码时,它从flikr加载json并将其包装在jsonp_callback_92661()中.这是唯一的区别.可能它是我问题的根源?
是的,那是你问题的根源.jsonp
当你不需要时,你正在使用它.由于您自己提供静态json文件,并且可能在主应用程序所在的域中提供服务,因此您不需要使用jsonp.如果您还不熟悉,那么关于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;
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3306 次 |
最近记录: |