我已阅读 cookbook 中的 RequestHandler
部分。有 isXml()
、isRss()
等,但没有 isJson()
。
还有其他方法可以检查请求是否为 JSON?
因此,当网址为 mysite.com/products/view/1.json
时,它将提供 JSON 数据,但如果没有 .json
,它将提供 HTML View 。
谢谢
最佳答案
我不认为 cakePHP 有像 isJson() 这样的用于 json 数据的函数,不过你可以创建你的自定义函数,例如:
//may be in your app controller
function isJson($data) {
return (json_decode($data) != NULL) ? true : false;
}
//and you can use it in your controller
if( $this->isJson($your_request_data) ) {
...
}
添加: 如果您想检查 .json 扩展名并进行相应处理,那么您可以在 Controller 中执行以下操作:
$this->request->params['ext']; //which would give you 'json' if you have .json extension
关于json - Cakephp - 如果请求是 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17057046/