首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生产模式下的CompoundJS呈现网页,而不是检索JSON数据

生产模式下的CompoundJS呈现网页,而不是检索JSON数据
EN

Stack Overflow用户
提问于 2013-05-11 04:04:45
回答 1查看 291关注 0票数 2

我是CompoundJS新手,我不太确定这是否是正确的行为。

我以PROD模式启动服务器,代码如下:

NODE_ENV=production compound server 8081

然后我撞到:

http://localhost:8081/categories/

我很想看到从服务器中检索到一些JSON

相反,它呈现如下页面:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-07 22:02:28

正如@Pablo在注释中提到的,只需在对控制器的调用中使用.json即可。

如下所示:

代码语言:javascript
复制
GET http://localhost:3000/categories.json

预期您的控制器将同时处理这两种情况,就像生成的控制器一样。

一个具体的例子:[approot]/app/controllers/category_controller.js

在JavaScript中:

代码语言:javascript
复制
action(function index() {
    this.title = 'Categories index';
    Category.all(function (err, categories) {
        respondTo(function (format) {
            // Use format.json and the send method to return JSON data when 
            // .json is specified at the end of the controller
            format.json(function () {
                send({code: 200, data: categories});
            });
            format.html(function () {
                render({
                   categories: categories
                });
            });
        });
    });
});

在CoffeeScript中:

代码语言:javascript
复制
action index = ->
  @title = "Categories index"
  Category.all (err, categories) ->
    respondTo (format) ->

      # Use format.json and the send method to return JSON data when 
      # .json is specified at the end of the controller
      format.json ->
        send
          code: 200
          data: categories


      format.html ->
        render categories: categories
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16493632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档