NodeJS 和 Backbone 的获取

发布于 2024-12-23 01:21:45 字数 923 浏览 0 评论 0原文

这是我的前端代码(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆兒滾 2024-12-30 01:21:45

那么 {test:1} 不是有效的 JSON。

{“测试”:“1”}
或者
{“测试”:1}
但是,请尝试其中之一。

键是 JSON 中的字符串,JSON 中的字符串必须用双引号括起来,请查看 JSON.org 了解更多信息。

为了确保您拥有针对更复杂对象的有效 JSON,只需使用 JSON.stringify()

var obj = { test : 1 };
response.write(JSON.stringify(obj)); //returns "{"test":1}"

另外,json 的正确 Content-Typeapplication/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():

var obj = { test : 1 };
response.write(JSON.stringify(obj)); //returns "{"test":1}"

Also, the correct Content-Type for json is application/json

眼眸 2024-12-30 01:21:45

{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

盗梦空间 2024-12-30 01:21:45

如果您使用的是express,则需要res.send 会自动将对象转换为JSON。如果您担心这一点,有一个名为 res.json 的新工具可以将任何内容转换为 JSON。

var obj = {super: "man"}
res.send(obj) // converts to json
res.json(obj) // also converts to 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.

var obj = {super: "man"}
res.send(obj) // converts to json
res.json(obj) // also converts to json

You don't need need writeHead(), write(), or end().

http://expressjs.com/guide.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文