admin管理员组文章数量:1321440
I have a tiny demo and it attempts to read app/data.json
using the Angular HttpClient.
const post$:Observable<Post> = <Observable<Post>> http.get('./data.json');
However the HttpClient
reponse says:
Failure during parsing ...
Thoughts?
I have a tiny demo and it attempts to read app/data.json
using the Angular HttpClient.
const post$:Observable<Post> = <Observable<Post>> http.get('./data.json');
However the HttpClient
reponse says:
Failure during parsing ...
Thoughts?
Share Improve this question asked Jan 5, 2019 at 7:30 OleOle 47.3k70 gold badges237 silver badges443 bronze badges3 Answers
Reset to default 8Stackblitz currently doesn't serve static files except the case when they are in assets
folder.
So you have two options here:
1) Import json directly as module
import data from './data.json';
console.log(data) // => {title: "Simulating HTTP Requsts", content: "This is off the hook!!"}
For more details See other answers
2) Move that json in assets folder(Note: you have to reload stackblitz to make it working):
http.get('/assets/data.json')
Forked Stackblitz
Currently, you can't get the JSON directly over HTTP, but you can import it instead
data.json
it returns resource of index.html instead of the data you expected
online example
import { of } from 'rxjs';
import data from './data.json';
export class AppComponent {
constructor(http:HttpClient) {
}
ngOnInit(){
const post$ = of(data);
post$.subscribe(console.log);
}
}
i think there are some issues about reading local json in stackblitz it doesn't return plain json just the index.html instead. but another way is mocking a request from local json, you can try:
import data from './data.json'
ngOnInit(){
this.getDatas().subscribe(data=>{
console.log(data)
})
}
getDatas():Observable<any>{
return of(data).pipe(delay(1000));
}
forked DEMO
本文标签: javascriptReading datajson with HttpClient on StackblitzStack Overflow
版权声明:本文标题:javascript - Reading data.json with HttpClient on Stackblitz? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742101361a2420815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论