Rails: format.js or format.json, or both?

Arslan AliBen提出了一个问题:Rails: format.js or format.json, or both?,或许与您遇到的问题类似。

回答者Zachbonyiii给出了该问题的处理方式:

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.

if format.json

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

server send:

data:application/json;base64,

if format.js

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

server send:

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

希望本文对你有帮助,欢迎支持JavaScript中文网


相关推荐