为什么在 $http.jsonp (angularjs) 中从未调用成功函数
How come success function is never called in $http.jsonp (angularjs)
我正在使用 ngTable 动态显示 json 文件中的数据并更新 table。但是,我的代码不起作用。
下面是一些代码片段:
$http.jsonp("http://127.0.0.1:3000/sampledate.json")
.success(function(data) {
//console.log(data);
$scope.user = data; });
这是 html table (ngTable) 的示例:
<div ng-controller="tableController">
<table ng-table="tableParams" show-filter="true" class="table table-striped" infinite-scroll-distance="3">
<tr ng-repeat="user in $data">
<td data-title="'Id'" sortable="'id'">
{{user.id}}
</td>
<td data-title="'File Name'" sortable="'file_name'" filter="{ 'file_name': 'text' }">
{{user.file_name}}
</td>
<td data-title="'Type'" sortable="'type'" filter="{ 'type': 'text' }">
{{user.type}}
</td>
<td data-title="'File Date'" sortable="'file_date'" filter="{ 'file_date': 'text' }">
{{user.file_date}}
</td>
<td data-title="'Hour'" sortable="'hour'" " filter="{ hour: 'number'}">
{{user.hour}}
</td>
<td data-title="'Bucket'" sortable="'bucket'">
{{user.bucket}}
</td> </tr></table></div>
人们可以给我一些解决此问题的想法吗?
或者给我一些关于完成我的任务的想法。
我的问题不是网络问题。见下文。
请求URL:http://127.0.0.1:3000/sampledate.json
请求 Method:GET
状态 Code:200 正常(来自缓存)
远程 Address:127.0.0.1:3000
看来这是错误的:
<tr ng-repeat="user in $data">
在这种情况下,您需要使用 $scope.data = data;
而不是 $scope.user = data;
此外,tr
中的 $data
应该是 data
。
最好叫它 $scope.users = data
,这样你就可以得到 ng-repeat="user in users"
只需添加一个错误函数并查看系统抛出的错误。
$http.jsonp("http://127.0.0.1:3000/sampledate.json").success(function(data, status, headers, config) {
//console.log(data);
$scope.user = data;
}).error(function(data, status, headers, config) {
$scope.error = true;
//Here, the console.log that you want (with status, headers... your choice)
console.log(status)
});
但我认为你的问题是关于代码的变量,而不是请愿书。
您从 jsonp
调用中检索看起来像单个用户的内容,然后将其存储到 $scope.user
,但在 html 中您调用了重复 $data
.
如果您的目标是从您的通话中获得多个用户,然后遍历他们,您必须更改两件事:
首先,将结果放入 $scope 中的值:
$scope.users = data;
然后通过更改标签在 html 中循环它:
<tr ng-repeat="user in users">
这现在应该可以工作了,因为它会在 jsonp
正确调用后循环遍历您设置的 $scope.users
。
我正在使用 ngTable 动态显示 json 文件中的数据并更新 table。但是,我的代码不起作用。 下面是一些代码片段:
$http.jsonp("http://127.0.0.1:3000/sampledate.json")
.success(function(data) {
//console.log(data);
$scope.user = data; });
这是 html table (ngTable) 的示例:
<div ng-controller="tableController">
<table ng-table="tableParams" show-filter="true" class="table table-striped" infinite-scroll-distance="3">
<tr ng-repeat="user in $data">
<td data-title="'Id'" sortable="'id'">
{{user.id}}
</td>
<td data-title="'File Name'" sortable="'file_name'" filter="{ 'file_name': 'text' }">
{{user.file_name}}
</td>
<td data-title="'Type'" sortable="'type'" filter="{ 'type': 'text' }">
{{user.type}}
</td>
<td data-title="'File Date'" sortable="'file_date'" filter="{ 'file_date': 'text' }">
{{user.file_date}}
</td>
<td data-title="'Hour'" sortable="'hour'" " filter="{ hour: 'number'}">
{{user.hour}}
</td>
<td data-title="'Bucket'" sortable="'bucket'">
{{user.bucket}}
</td> </tr></table></div>
人们可以给我一些解决此问题的想法吗? 或者给我一些关于完成我的任务的想法。
我的问题不是网络问题。见下文。
请求URL:http://127.0.0.1:3000/sampledate.json 请求 Method:GET 状态 Code:200 正常(来自缓存) 远程 Address:127.0.0.1:3000
看来这是错误的:
<tr ng-repeat="user in $data">
在这种情况下,您需要使用 $scope.data = data;
而不是 $scope.user = data;
此外,tr
中的 $data
应该是 data
。
最好叫它 $scope.users = data
,这样你就可以得到 ng-repeat="user in users"
只需添加一个错误函数并查看系统抛出的错误。
$http.jsonp("http://127.0.0.1:3000/sampledate.json").success(function(data, status, headers, config) {
//console.log(data);
$scope.user = data;
}).error(function(data, status, headers, config) {
$scope.error = true;
//Here, the console.log that you want (with status, headers... your choice)
console.log(status)
});
但我认为你的问题是关于代码的变量,而不是请愿书。
您从 jsonp
调用中检索看起来像单个用户的内容,然后将其存储到 $scope.user
,但在 html 中您调用了重复 $data
.
如果您的目标是从您的通话中获得多个用户,然后遍历他们,您必须更改两件事:
首先,将结果放入 $scope 中的值:
$scope.users = data;
然后通过更改标签在 html 中循环它:
<tr ng-repeat="user in users">
这现在应该可以工作了,因为它会在 jsonp
正确调用后循环遍历您设置的 $scope.users
。