Javascript 生产模式下的CompoundJS呈现网页,而不是检索JSON数据
我是刚接触CompoundJS的,我不太确定这是否是正确的行为 我正在以PROD模式启动服务器,代码如下:Javascript 生产模式下的CompoundJS呈现网页,而不是检索JSON数据,javascript,node.js,compoundjs,Javascript,Node.js,Compoundjs,我是刚接触CompoundJS的,我不太确定这是否是正确的行为 我正在以PROD模式启动服务器,代码如下: 节点_ENV=生产复合服务器8081 然后我点击: http://localhost:8081/categories/ 我意外地看到从服务器中检索到一些JSON 相反,它呈现如下页面: 正如@Pablo在评论中提到的,只需在调用控制器时使用.json 具体如下: GET http://localhost:3000/categories.json 预期您的控制器将处理这两个问题,就
节点_ENV=生产复合服务器8081
然后我点击:
http://localhost:8081/categories/
我意外地看到从服务器中检索到一些JSON
相反,它呈现如下页面:
正如@Pablo在评论中提到的,只需在调用控制器时使用
.json
具体如下:
GET http://localhost:3000/categories.json
预期您的控制器将处理这两个问题,就像在生成的控制器中一样
一个具体的例子:[approt]/app/controllers/category\u controller.js
在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
});
});
});
});
});
在咖啡脚本中:
你为什么期待JSON响应?嗨,Robert,我期待JSON响应,这样我就可以在前端应用程序中使用它了。另外,我不想把这个界面暴露给别人。。(有意义吗?)有意义,但您必须告诉控制器生成JSON,而不是呈现模板。您可以尝试使用
send(您的_对象)
使其返回JSON,而不是render()
。谢谢!现在我看到了它在幕后是如何运作的。我也可以打电话给http://localhost:8081/categories.json
这样就可以了。啊,那更好:)我对CompoundJS不太了解,所以我不知道.json
技巧:)
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