Cakephp プロジェクトに承認を実装したため、JSON を使用してビューにアクセスできなくなりました。これを再度機能させるには、特定の設定をどこかに設定する必要がありますか? allowedActions と isAuthorized に autoComplete を追加することでうまくいくことを期待していました
app\Controller\BrandsController.php (不要なコードを削除)
<?php
App::uses('Sanitize', 'Utility');
class BrandsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allowedActions = array('autoComplete');
}
public function isAuthorized($user) {
if ($this->action === 'view' || $this->action === 'index' || $this->action === 'autoComplete') {
return true;
}
return parent::isAuthorized($user);
}
public function autoComplete($name = null)
{
if(!$name)
{
$name = @$this->params->query['name'];
}
$name = Sanitize::paranoid($name, array(' '));
if (!$name || empty($name))
{
new NotFoundException(__('Invalid searchquery'));
}
else
{
$objects = $this->Brand->find('list',
array(
'conditions' => array("Brand.name LIKE" => "%" . $name . "%")
)
);
$this->layout = 'ajax';
$this->RequestHandler->setContent('json', 'application/json' );
$this->set(compact('objects'));
$this->render('json/output_json');
}
}
}
?>
app\View\Brands\json\output_json.ctp
<?php
Configure::write('debug', 0);
echo json_encode($objects);
?>
電話
<?php
$brandsAutoCompleteUrl = Router::url(array('controller' => 'brands', 'action' => 'autoComplete'));
?>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON('<?php echo $brandsAutoCompleteUrl; ?>', { name: 'test' }, function(data) {
// never called
});
});
</script>
私のChromeデバッグウィンドウでは、
GET http://localhost/cakephp/brands/autoComplete?name=tes 500 (Internal Server Error)
この URL をブラウザから直接呼び出すと、期待どおりの結果が得られます