我在sails中看到,在调用的请求对象中有一个变量,wantsJSON在创建新项目时生成的错误页面中,它显示
if (req.wantsJSON) {
//Do Something
}
Run Code Online (Sandbox Code Playgroud)
帆如何决定用户是否想要JSON?
Sails根据请求决定.您可以在源代码中找到它.我复制并评论了有趣的部分:
// 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'));
Run Code Online (Sandbox Code Playgroud)