为什么$http.jsonp(angularjs)中从未调用成功函数
我使用ngTable动态显示json文件中的数据并更新表。但是,我的代码不起作用。 下面是一些代码片段:为什么$http.jsonp(angularjs)中从未调用成功函数,angularjs,ngtable,Angularjs,Ngtable,我使用ngTable动态显示json文件中的数据并更新表。但是,我的代码不起作用。 下面是一些代码片段: $http.jsonp("http://127.0.0.1:3000/sampledate.json") .success(function(data) { //console.log(data); $scope.user = data; }); 以下是html表(ngTable)的示例: {{user.id} {{user
$http.jsonp("http://127.0.0.1:3000/sampledate.json")
.success(function(data) {
//console.log(data);
$scope.user = data; });
以下是html表(ngTable)的示例:
{{user.id}
{{user.file_name}
{{user.type}
{{user.file_date}
看起来这是错误的:
<tr ng-repeat="user in $data">
在这种情况下,您需要使用$scope.data=data
而不是$scope.user=data代码>
另外,tr
中的$data
也应该是data
最好将其命名为$scope.users=data
,这样您就得到了ng repeat=“user in users”
看起来这是错误的:
<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)
});
但是我认为您的问题是代码的变量,而不是请求。只需添加一个错误函数,然后查看系统抛出的错误
$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
。从jsonp
调用中检索看起来像单个用户的内容,然后将其存储到$scope.user
中,但在html中调用repeat on$data
如果您的目标是从您的通话中获得多个用户,然后通过他们进行循环,那么您必须更改两件事:
首先,将结果放入$scope中的值中:
$scope.users = data;
然后通过更改标记在html中循环:
<tr ng-repeat="user in users">
现在应该可以工作了,因为它将循环通过jsonp
调用后正确设置的$scope.users
。不能对静态json文件使用jsonp
请求。同样,对于$http.jsonp
,您必须使用文档中列出的回调字符串,并且没有任何变化。您尚未提供任何回调参数。由于请求是由您控制的api,我怀疑您是否想要jsonp。这是跨端口/跨域请求吗?是。以前它是一个跨领域的问题。现在,因为我使用了$http.jsonp,所以应该不再是那个问题了。我使用了$http.get,当时出现了跨域问题。在服务器上实现CORS。或者,要使用jsonp,您需要一个服务器脚本来从文件中提取内容并将其包装到jsonp回调函数中,并且您需要将callback
参数添加到URL中。不能对静态json文件使用jsonp
请求。同样,对于$http.jsonp
,您必须使用文档中列出的回调字符串,并且没有任何变化。您尚未提供任何回调参数。由于请求是由您控制的api,我怀疑您是否想要jsonp。这是跨端口/跨域请求吗?是。以前它是一个跨领域的问题。现在,因为我使用了$http.jsonp,所以应该不再是那个问题了。我使用了$http.get,当时出现了跨域问题。在服务器上实现CORS。或者,要使用jsonp,您需要一个服务器脚本来从文件中提取内容并将其包装到jsonp回调函数中,您需要将callback
参数添加到URL中。读取jsonp文档会发现您缺少了回调查询参数等重要部分。还要注意的是,success
和error
在me文档中已被弃用,现在似乎可以工作了。不知道为什么它没有。。。谢谢阅读jsonp文档会发现您缺少了回调查询参数等重要部分。还要注意的是,success
和error
在me文档中已被弃用,现在似乎可以工作了。不知道为什么它没有。。。谢谢