【发布时间】:2018-02-21 12:24:09
【问题描述】:
我一直在尝试通过 AJAX 向控制器发送一些数据,但对于我来说,我似乎无法让它工作;每次我发出请求时,都会抛出 403 禁止错误。
这是 ajax 请求:
$.ajax({
type: 'post',
url:"<?php echo Router::url(array('controller'=>'Atls','action'=>'saveTime', '_ext' => 'json'));?>",
dataType: 'json',
data: {atl_id: idTimerPaused, time: actual_time},
beforeSend: function(xhr){
},
success: function (response) {
console.log('Nailed It');
},
error: function(jqXHR, exception){
console.log(jqXHR);
}
});
return false;
控制器动作:
public function saveTime()
{
if ($this->request->is('post') && $this->request->is('ajax')) {
$content = $this->request->getData();
$query = $this->Atls->query();
$result = $query
->update()
->set(
$query->newExpr('actual_time = '. $content['time'])
)
->where([
'id' => $content['atl_id']
])
->execute();
$this->set(compact('content'));
$this->set('_serialize', ['content']);
$this->render('ajax_response', 'ajax');
}
}
我已经在 routes.php 文件中加载了扩展 (Router::extensions('json', 'xml');)
请求处理程序也被加载并允许该函数:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow('saveTime');
//Change layout for Ajax requests
$this->viewBuilder()->layout('appTemplate');
if ($this->request->is('ajax')) {
$this->viewBuilder()->layout('ajax');
}
}
“ajax_response”视图也已添加。
我看不出问题出在哪里。因此,我将不胜感激任何帮助我解决这个问题。
【问题讨论】:
-
嗨,您的服务器上是否启用了任何 .htacess 和 URL 重写?
-
@headmax .htaccess 是框架自带的通用的,没有被修改过。我启用了 mod_rewrite。
-
任何文件夹权限受限?在进程调用中不如755?您的 apache 或 php 日志中是否有任何错误?
-
这是最让我沮丧的,日志上没有显示错误,所有文件夹都是 755。
-
看看文件调用的权限 if is 644 :( ?
标签: php jquery ajax cakephp-3.0 http-status-code-403