CakePHP Dropzone.js错误Ajax
我正在尝试将dropzone.js实现到我的CakePHP应用程序中。到目前为止一切顺利。除非收到错误,否则它将显示整个HTML错误页面,而不是呈现。这会变成一堆HTML代码,不太可读,而且由于错误框变得太大,我无法单击“删除”按钮。见下图: 一旦我收到错误: 当我在收到错误后将框悬停时: 我知道原因是dropzone.js会识别错误,因为Ajax页面的500页眉会在出现问题时引发异常。CakePHP为一个500错误页面呈现一个完整的布局。所以我不可能只查看一行错误。我真的需要500个标题,因为其他dropzone.js认为一切都很顺利 所以我的问题是:当在一个特定的控制器方法中得到一个500错误时,有可能不呈现500错误布局吗?我不想完全禁用500错误布局渲染。仅适用于AJAX页面CakePHP Dropzone.js错误Ajax,php,ajax,cakephp,dropzone.js,Php,Ajax,Cakephp,Dropzone.js,我正在尝试将dropzone.js实现到我的CakePHP应用程序中。到目前为止一切顺利。除非收到错误,否则它将显示整个HTML错误页面,而不是呈现。这会变成一堆HTML代码,不太可读,而且由于错误框变得太大,我无法单击“删除”按钮。见下图: 一旦我收到错误: 当我在收到错误后将框悬停时: 我知道原因是dropzone.js会识别错误,因为Ajax页面的500页眉会在出现问题时引发异常。CakePHP为一个500错误页面呈现一个完整的布局。所以我不可能只查看一行错误。我真的需要500个标题,因
public function admin_add($slug = null) {
if(!$slug || !$client = $this->Video->Client->find('first', array('conditions' => array('slug' => $slug)))) {
throw new NotFoundException(__('Invalid client'));
}
if ($this->request->is('post')) {
// If request contains files, continue
if (!empty($_FILES)) {
// Get slug from URL
$slug = substr( $this->referer(), strrpos( $this->referer(), '/' )+1 );
// Create new folder for the movies if it doesn't exist already
if (!file_exists(WWW_ROOT.'/files/'.$slug)) {
mkdir(WWW_ROOT.'/files/'.$slug, 0777, true);
}
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = '/files/'.$slug.'/';
$targetFile = $targetPath. $_FILES['file']['name'];
// Create variable filename without the extension
$fileWithoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $_FILES['file']['name']);
// Add file to Video array
$video['Video'] = array('video' => $targetFile, 'screenshot' => '/files/'.$slug.'/screenshots/'.$fileWithoutExt.'.jpg', 'client_id' => $client['Client']['id']);
// unset($video);
// Try moving the file to their final directory
if(!move_uploaded_file($tempFile, WWW_ROOT.$targetFile)) {
throw new NotFoundException(__('Move image to "'.WWW_ROOT.$targetPath.'" failed'));
}
// Create new folder for the screenshots if it doesn't exist already
if (!file_exists(WWW_ROOT.'/files/'.$slug.'/screenshots/')) {
mkdir(WWW_ROOT.'/files/'.$slug.'/screenshots/', 0777, true);
}
// Try saving video to Video table in the database
if(!$this->Video->save($video)){
throw new NotFoundException(__('Failed connecting client with "'.$targetFile.'" in the database'));
}
}
$this->Session->setFlash(__('Videos successfully uploaded'), 'default', array(), 'success');
$this->redirect($this->referer());
}
$title_for_layout = $client['Client']['name'];
$this->set(compact('title_for_layout', 'client'));
}
您可以使用CakerResponse类的方法更改返回的状态代码。类似这样:$this->response->statusCode404;
使用NotFoundException返回http状态代码有点不正确。至少您可以创建自己的应用程序异常
请查收
您将很容易定义一个异常:
class MissingWidgetException extends CakeException {};
之后,您可以使用它并发送所需的http状态代码:
501是http状态代码
希望,这将有助于找到正确的解决方案。简短回答:是的,这是可能的。如果你提供更多的源代码,我们可以提供更具体的建议。好的,我将发布CakePHP方法,用于将dropzone.js上传到数据库。这个函数会创建异常,从而产生500个错误,明天我一到办公室就可以了。谢谢!到目前为止一切顺利。除非我知道我真的需要一个500错误,以防止dropzone.js完成上传。它只在500错误处停止。那么,是否有可能仅为当前的controlle方法创建一个自定义500布局?我可以在错误中更改500.ctp,但我希望其他控制器方法的布局…500错误它只是服务器返回http状态代码500,因此,您可以使用$this->response->statusCode500;并呈现您真正需要的内容。更多关于如何返回http状态码的详细信息,您可以在这里找到:好的!现在它工作得很好:我再也没有那么多代码了:谢谢你的帮助!
throw new MissingWidgetHelperException('Its not here', 501);