我在sails中看到请求对象中有一个名为wantsJSON
的变量,并且在创建新项目时生成的错误页面中显示
if (req.wantsJSON) {
//Do Something
}
sails如何决定用户是否想要JSON?
答案 0 :(得分:7)
Sails根据请求决定。您可以在the source code中找到它。我已经复制并评论了有趣的部分:
// True, if the request has a xhr polling origin. (via socket)
req.wantsJSON = req.xhr;
// True, if the above was false (or null) and the origin not only accepts html.
// See HTTP header field 'Accept'
req.wantsJSON = req.wantsJSON || !req.explicitlyAcceptsHTML;
// True, if everything above was false (or null) and the origins content type
// is json and the header field 'Accept' is set.
// See HTTP header field 'Content-type'
req.wantsJSON = req.wantsJSON || (req.is('json') && req.get('Accept'));