我正在为JSON运行$ http.get并且状态为0.我已经下载了相同的JSON并且get在本地工作,而在Python中使用请求库我可以得到JSON没问题,但是AngularJS它没有用.我不明白为什么角度没有得到它,但其他一切都是.下面的代码片段.
function AgentListCtrl($scope, $http) {
$http.get('http://foo.bar/api/objects').success(function(data) {
$scope.objects = data;
}).error(function(data, status) {
$scope.status1 = status;
});
Run Code Online (Sandbox Code Playgroud)
这提供了JSON并在使用本地文件时对其进行解析,但否则会失败并将status1设置为0.
我有一个用Angular4编写的测试数据服务.它目前看起来像这样:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise'
@Injectable()
export class DataService {
constructor(private http: Http) { }
fetchData(){
return this.http.get('https://dinstruct-d4b62.firebaseio.com/.json').map(
(res) => res.json()).toPromise();
}
}
Run Code Online (Sandbox Code Playgroud)
感谢这个代码的"网络忍者",因为该应用程序的这一部分基本上与教程代码完全相同(我更喜欢在构建新应用程序时有一些应该是用于测试目的的已知工作示例). .
问题是虽然在https://dinstruct-d4b62.firebaseio.com/.json上肯定存在测试数据,但就我所知(可通过浏览器直接访问)而言,它并未以任何方式隐藏或防火墙,应用程序进入该fetchData()功能,它记录:
ERROR Error: Uncaught (in promise): Error: Response with status: 404 Not Found for URL: https://dinstruct-d4b62.firebaseio.com/.json
Error: Response with status: 404 Not Found for URL: https://dinstruct-d4b62.firebaseio.com/.json
Run Code Online (Sandbox Code Playgroud)
在堆栈跟踪的开始.这可能会发生什么?
更新:
我也注意到在调用函数中:
ngOnInit(): void {
this.customerService.getCustomers()
.then(customers => this.customers = customers);
this.dataService.fetchData().subscribe( …Run Code Online (Sandbox Code Playgroud)