Return 来自 NodeJS 服务器的 JSONP AngularJS
Return JSONP from NodeJS server with AngularJS
我正在尝试在我的应用程序上实施 ngInfiniteScroll - Loading Remote Data。
Demo 运行良好,我成功获得了 Reddit API,但我的列表无法运行。
我成功命中 200
服务器响应并且可以在开发工具中看到我的数据。这些答案提供了一些帮助 1 & 2 但我仍然不确定该怎么做。
编辑(参见 angular 工厂编辑):
此回调正在运行,$http
将会成功,但我现在得到 Cannot read property 'length' of undefined
Angular工厂:
Reddit.prototype.nextPage = function() {
if (this.busy) return;
this.busy = true;
// Edit - I changed this var from
// var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK";
// to
var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data) {
console.log(data);
var items = data.children;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i].data);
}
this.after = "t3_" + this.items[this.items.length - 1].id;
this.busy = false;
}.bind(this));
console.log('Reddit.prototype');
};
NodeJS 路线:
app.get('/list', function (req, res) {
Found.find(function (err, post) {
if ( err ) {
res.send( err );
}
res.jsonp( post );
});
});
Reddit.prototype.nextPage = function() {
if (this.busy) return;
this.busy = true;
// Edit - I changed this var from
// var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK";
// to
var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data) {
console.log(data);
var items = data;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i]);
}
this.after = "t3_" + this.items[this.items.length - 1].id;
this.busy = false;
}.bind(this));
console.log('Reddit.prototype');
};
应该修复它!
我正在尝试在我的应用程序上实施 ngInfiniteScroll - Loading Remote Data。
Demo 运行良好,我成功获得了 Reddit API,但我的列表无法运行。
我成功命中 200
服务器响应并且可以在开发工具中看到我的数据。这些答案提供了一些帮助 1 & 2 但我仍然不确定该怎么做。
编辑(参见 angular 工厂编辑):
此回调正在运行,$http
将会成功,但我现在得到 Cannot read property 'length' of undefined
Angular工厂:
Reddit.prototype.nextPage = function() {
if (this.busy) return;
this.busy = true;
// Edit - I changed this var from
// var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK";
// to
var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data) {
console.log(data);
var items = data.children;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i].data);
}
this.after = "t3_" + this.items[this.items.length - 1].id;
this.busy = false;
}.bind(this));
console.log('Reddit.prototype');
};
NodeJS 路线:
app.get('/list', function (req, res) {
Found.find(function (err, post) {
if ( err ) {
res.send( err );
}
res.jsonp( post );
});
});
Reddit.prototype.nextPage = function() {
if (this.busy) return;
this.busy = true;
// Edit - I changed this var from
// var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK";
// to
var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data) {
console.log(data);
var items = data;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i]);
}
this.after = "t3_" + this.items[this.items.length - 1].id;
this.busy = false;
}.bind(this));
console.log('Reddit.prototype');
};
应该修复它!