Rails:format.js或format.json,还是两者兼而有之? [英] Rails: format.js or format.json, or both?

查看:101
本文介绍了Rails:format.js或format.json,还是两者兼而有之?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能很明显,但我仍然缺乏基本知识。

Might be obvious, but still I'm lacking here of the basic knowledge.

所以内部控制器,都可以使用,或者它总是Javascript,所以两者是一样的吗?

So inside controllers, can both be used, or is it always Javascript, so both are the same?

推荐答案

在Rails 3.1中我唯一可以看出的区别是if格式化程序是js,那么答案以纯文本形式发送。如果是json那么它被编码为json。

In Rails 3.1 the only difference I can spot is that the if formatter is js, then the answer is sent as plain text. If it is json then it is encoded as json.

如果是format.json

if format.json

format.json {
  render :json => @accounts.map(&:attributes)
}

服务器发送:

data:application/json;base64,

如果format.js

if format.js

format.js {
  render :json => @accounts.map(&:attributes)
}

服务器发送:

[{"id":33,"email":"user1@example.com"}]

这篇关于Rails:format.js或format.json,还是两者兼而有之?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆