NodeJS 和 Backbone 的获取
这是我的前端代码(使用 fetch
)
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
url: '/questions',
model: MyModel
});
var coll = new MyCollection();
coll.fetch({
error: function (collection, response) {
console.log('error', response);
},
success: function (collection, response) {
console.log('success', response);
}
});
,这是我的后端代码(使用 app.get
)
app.get('/questions', function (request, response) {
console.log('Inside /questions');
response.writeHead(200, {
'Content-Type': 'text/json'
});
response.write('{test:1}');
response.end();
});
问题是,尽管响应符合预期,调用客户端 error
回调。当我删除 response.write('{test:1}');
行时,会调用 success
回调。关于我可能做错了什么有什么想法吗?
This is my front-end code (using fetch
)
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
url: '/questions',
model: MyModel
});
var coll = new MyCollection();
coll.fetch({
error: function (collection, response) {
console.log('error', response);
},
success: function (collection, response) {
console.log('success', response);
}
});
and this is my back-end code (using app.get
)
app.get('/questions', function (request, response) {
console.log('Inside /questions');
response.writeHead(200, {
'Content-Type': 'text/json'
});
response.write('{test:1}');
response.end();
});
The problem is that although the response is as expected, the client-side error
callback is called. When I remove the line response.write('{test:1}');
, the success
callback is called. Any ideas as to what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么
{test:1}
不是有效的 JSON。{“测试”:“1”}
或者
{“测试”:1}
但是,请尝试其中之一。
键是 JSON 中的字符串,JSON 中的字符串必须用双引号括起来,请查看 JSON.org 了解更多信息。
为了确保您拥有针对更复杂对象的有效 JSON,只需使用
JSON.stringify()
:另外,json 的正确 Content-Type 是
application/json
Well
{test:1}
is not valid JSON.{ "test":"1" }
OR
{ "test":1 }
is however, try one of those instead.
Keys are strings in JSON, and strings in JSON must be wrapped in double quotes check out JSON.org for more information.
To ensure you have valid JSON for more complex objects just use
JSON.stringify()
:Also, the correct Content-Type for json is
application/json
{test:1}
不是有效的 JSON,您应该尝试{ "test":"1" }
。另一个解决方案是检查 Express 的 render.json 函数,看看它如何将 json 发送到浏览器:
https://github.com/visionmedia/express/blob/master/lib/response.js#L152-172
{test:1}
isn't valid JSON, you should try{ "test":"1" }
.Another solution is to check Express's render.json function to see how it does sending json to the browser:
https://github.com/visionmedia/express/blob/master/lib/response.js#L152-172
如果您使用的是express,则需要res.send 会自动将对象转换为JSON。如果您担心这一点,有一个名为 res.json 的新工具可以将任何内容转换为 JSON。
您不需要 writeHead()、write() 或 end()。
http://expressjs.com/guide.html
If you're using express you need to res.send will automatically convert objects into JSON. If you're worried about it, there's a new one called res.json that will convert anything into JSON.
You don't need need writeHead(), write(), or end().
http://expressjs.com/guide.html