我正在localhost上的cakephp项目上工作,在其中使用jQuery在表中实现拖放重新排序。限制是只有管理员才能对表中的元素进行重新排序,如果用户不是管理员,则将显示错误消息。在localhost上,它按预期工作,但是当我将其部署在服务器上时,非管理员用户会收到成功消息,但未应用重新排序。所以我想响应有问题。
这是我的代码:
view.ctp
<script type="text/javascript">
var csrfToken = <?= json_encode($this->request->getParam('_csrfToken')) ?>;
$( ".row_position" ).sortable({
delay: 150,
stop: function() {
var selectedData = new Array();
$('.row_position>tr').each(function() {
selectedData.push($(this).attr("id"));
});
updateOrder(selectedData);
}
});
function updateOrder(data) {
$.ajax({
headers: {
'X-CSRF-Token': csrfToken
},
url:'/app/users/updateTasksOrder',
type:'post',
data:{position:data},
success:function(){
alert('Your change successfully saved');
location.reload();
},
error:function(){
alert('Your change failed. Only Admins or Project Managers can change the order');
location.reload();
}
})
}
</script>
controller.php
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
if($this->Auth->user('role_id')==1 or $this->Auth->user('role_id')==2){ //1 admin, 2 project manager
$this->Auth->allow('updateTasksOrder');
$this->set('is_admin', true);
}
else
{
if($this->request->is('ajax')) {
$this->response->type('json');
$this->response->statusCode(401);
$this->response->body(json_encode(array('status' => 'ERROR', 'message' => 'Unauthorized')));
$this->response->send();
$this->_stop();
}
if($this->request->getParam('action') === 'delete'){
$this->Flash->error(__('You are not authorized to perform this action'));
return $this->redirect(['controller' => 'Users', 'action' => 'index']);
}
$this->set('is_admin', false);
}
}