[英]Cannot get data from json-server
I'm making a little project in Backbone.js and I'm running a json-server
package to populate it wih some data. 我正在Backbone.js中创建一个小项目,并且正在运行
json-server
程序包以将其填充一些数据。 I've made a db.json
file with data and ran json-server --watch db.json
, it started and runs perfectly on localhost:3000
. 我已经用数据制作了一个
db.json
文件,并运行json-server --watch db.json
,它启动并在localhost:3000
上完美运行。 In my Backbone app I have this: 在我的Backbone应用程序中,我有以下内容:
// app/javascripts/collections/resumes.js
define(['backbone', 'models/resume'], function (Backbone, Resume) {
var ResumesCollection = Backbone.Collection.extend({
model: Resume,
url: 'http://localhost:3000/resumes'
});
return ResumesCollection;
});
// app/javascripts/views/resumes/resume.js
define(['backbone', 'jquery'], function (Backbone, $) {
var ResumeView = Backbone.View.extend({
tagName: 'article',
render: function () {
this.$el.html(this.model.get("firstName"));
return this;
}
});
return ResumeView;
});
// app/javascripts/views/resumes/index.js
define(['backbone', 'views/resumes/resume'], function (Backbone, ResumeView) {
var ResumesList = Backbone.View.extend({
tagName: 'section',
initialize: function() {
this.collection.fetch();
},
render: function() {
var resumesView = this.collection.map(function (cv) {
return new ResumeView({model: cv}).render().el;
});
this.$el.html(resumesView);
return this;
}
});
return ResumeList;
});
this is my app/router.js
: 这是我的
app/router.js
:
define(['backbone',
'collections/resumes',
'views/resumes/resume',
'views/resumes/index'],
function (Backbone, ResumesCollection, ResumeView, ResumeList) {
var AppRouter = Backbone.Router.extend({
routes: {
'resumes': 'showAll'
},
showAll: function () {
this.ResumeList.render();
}
});
return AppRouter;
});
and in app/javascripts/main.js
I have this: 在
app/javascripts/main.js
我有这个:
require.config({
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
},
paths: {
jquery: '../node_modules/jquery/dist/jquery',
underscore: '../node_modules/underscore/underscore',
backbone: '../node_modules/backbone/backbone'
}
});
require(['backbone',
'jquery',
'router'
], function (Backbone, $, AppRouter) {
var Router = new AppRouter();
Backbone.history.start({
pushState: true,
root: '/'
});
});
Also I use Gulp to run a development server on localhost:8080
via gulp-connect
and gulp-livereload
. 我也使用Gulp通过
gulp-livereload
gulp-connect
和gulp-livereload
在localhost:8080
上运行开发服务器。 But when I navigate to localhost:8080/resumes
, it returns me Cannot GET /resumes
, though there's no errors in console. 但是,当我导航到
localhost:8080/resumes
Cannot GET /resumes
,尽管控制台中没有错误,但它返回我Cannot GET /resumes
。 What am I doing wrong?+ 我在做什么错?+
With what's provided, we can't pinpoint the exact problem and cause, but let me try to help anyway. 通过所提供的内容,我们无法查明确切的问题和原因,但是无论如何,我还是要尝试提供帮助。
I tried json-server and started it with the following db.json
: 我尝试了json-server并使用以下
db.json
启动它:
{
"posts": [{
"id": 1,
"title": "json-server",
"author": "typicode"
}, {
"id": 2,
"title": "This is a test",
"author": "Emile Bergeron"
}]
}
Then I made the simplest collection to use it. 然后,我制作了最简单的收藏夹来使用它。
var PostCollection = Backbone.Collection.extend({
url: "http://localhost:3000/posts"
});
var posts = new PostCollection();
posts.fetch({
success: function(){
console.log(posts.pluck('title'));
}
});
And in my console, I can see: 在控制台中,我可以看到:
["json-server", "This is a test"]
Using it was surprisingly straightforward! 使用它非常简单! So the problem is elsewhere.
因此问题出在其他地方。 Maybe show us the full request (raw data).
也许向我们显示完整的请求(原始数据)。
this.collection.fetch(); // this is enough
There's no need to pass { data: { fetch: true, type:"get" } }
as it is the default behavior anyway. 无需传递
{ data: { fetch: true, type:"get" } }
因为它仍然是默认行为。
The following line inside ResumeList
will fail: ResumeList
的以下行将失败:
return new ResumeView({model: cv}).render().el;
This is because the render
function of the ResumeView
doesn't return this
. 这是因为
ResumeView
的render
函数不会返回this
。
Overall, your code looks good. 总体而言,您的代码看起来不错。
If you're going to put the API on another server, thus another domain, take a look at CORS which will be needed to enable js to fetch a different domain. 如果要将API放置在另一台服务器(即另一台域)上,请查看CORS ,这将使js能够获取其他域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.