Javascript CakePHP3.1-获得Ajax的操作返回成功
我正在尝试使用ajax向数据库添加数据,但这是我第一次尝试使用ajax,这就是我无法解决此问题的原因 我有一个add()操作,它检查表单是否已发送。如果是,它会向数据库中添加一条记录Javascript CakePHP3.1-获得Ajax的操作返回成功,javascript,php,jquery,ajax,cakephp,Javascript,Php,Jquery,Ajax,Cakephp,我正在尝试使用ajax向数据库添加数据,但这是我第一次尝试使用ajax,这就是我无法解决此问题的原因 我有一个add()操作,它检查表单是否已发送。如果是,它会向数据库中添加一条记录 public function add() { if ($this->request->is('ajax')) { Configure::write('debug', 0); } $this->loadComponent('RequestHandler');
public function add()
{
if ($this->request->is('ajax')) {
Configure::write('debug', 0);
}
$this->loadComponent('RequestHandler');
$this->loadModel('Galleries');
$entity = $this->Galleries->newEntity();
if ($this->request->is('post'))
{
/*$photos = $this->request->data['multiple_photos'];
for ($i = 0; $i < count($photos); $i++) {
$photo = [
'name' => $this->request->data['multiple_photos'][$i]['name'],
'type' => $this->request->data['multiple_photos'][$i]['type'],
'tmp_name' => $this->request->data['multiple_photos'][$i]['tmp_name'],
'error' => $this->request->data['multiple_photos'][$i]['error'],
'size' => $this->request->data['multiple_photos'][$i]['size']
];
echo "<pre>"; print_r($photo); echo "</pre>";
}*/
$data = [
'id' => '',
'thumb' => 'thuburl',
'highres' => 'highresurl',
'gal_id' => 1
];
$entity = $this->Galleries->patchEntity($entity, $data);
$this->Galleries->save($entity);
}
$this->set(compact('entity'));
$this->viewBuilder()->layout(false);
$this->render();
}
公共函数添加()
{
如果($this->request->is('ajax')){
Configure::write('debug',0);
}
$this->loadComponent('RequestHandler');
$this->loadModel(“画廊”);
$entity=$this->Galleries->newEntity();
如果($this->request->is('post'))
{
/*$photos=$this->request->data['multiple_photos'];
对于($i=0;$ithis->request->data['multiple_photos'][$i]['name'],
'type'=>this->request->data['multiple_photos'][$i]['type'],
'tmp_name'=>this->request->data['multiple_photos'][$i]['tmp_name'],
'error'=>this->request->data['multiple_photos'][$i]['error'],
'size'=>this->request->data['multiple_photos'][$i]['size']
];
echo“;print_r($photo);echo”;
<body>
<?php
echo $this->Form->create($entity, ['type' => 'file', 'id' => 'formUpload', 'action' => 'add']);
echo $this->Form->hidden('id', ['value' => '']);
echo $this->Form->input('multiple_photos[]', ['type' => 'file', 'multiple' => 'true', 'label' => false]);
echo $this->Form->hidden('thumb', ['value' => 'thumburl']);
echo $this->Form->hidden('highres', ['value' => 'highresurl']);
echo $this->Form->hidden('gal_id', ['value' => '1']);
echo $this->Form->button('Wyslij', ['type' => 'submit']);
echo $this->Form->end();
?>
<script>
$('#formUpload').submit(function() {
var formData = $(this).serialize();
var formUrl = $(this).attr('action');
$.ajax({
type: "POST",
url: formUrl,
data: formData,
success: function(data,textStatus,xhr) {
alert(data);
},
error: function(xhr,textStatus,error) {
alert(textStatus);
}
});
return false;
});
</script>
</body>
}*/
$data=[
“id'=>”,
“thumb”=>“Thubur”,
“highres”=>“highresurl”,
“gal_id”=>1
];
$entity=$this->Galleries->patchEntity($entity,$data);
$this->gallers->save($entity);
}
$this->set(压缩(“实体”);
$this->viewBuilder()->layout(false);
$this->render();
}
我认为:
$('#formUpload')。提交(函数(){
var formData=$(this.serialize();
var formUrl=$(this.attr('action');
$.ajax({
类型:“POST”,
url:formUrl,
数据:formData,
成功:函数(数据、文本状态、xhr){
警报(数据);
},
错误:函数(xhr、textStatus、error){
警报(文本状态);
}
});
返回false;
});
结果如何?它添加了一条记录,因此Ajax脚本可以工作,但我收到了“错误”警报
如何修改动作或ajax脚本以使其正常工作
提前感谢:)您的脚本返回的响应状态是什么?我得到404未找到状态。使其工作正常,只是在控制器中将操作更改为新操作。现在我只需要通过ajax请求传递表单数据,因为我得到的是空输入。