这是我的.ctp文件,点击提交按钮,它应该将数据保存在数据库中。但是当函数在控制器上运行时,它不会返回任何值,所以它不会在ajax的成功函数中运行。我不知道它是否会进入控制器函数并返回任何值。
$('#submit_btn').click(function(e){
var value1=$('#nname').val();
//alert(value1);
$.ajax({
cache: false,
dataType: "html",
type: "POST",
evalScripts: true,
url: '<?php echo Router::url(array('controller'=>'Partconfs','action'=>'addlot'));?>',
data: ({name1:value1}),
success: function(result){
console.log(result);
alert(result);
if(result==2)
{
$('*').css('cursor','auto');
}
}
});
});关联的.php文件,我只是简单地将lot_no保存在数据库的批次表中并回显2。因此,它应该返回到ajax.but的成功函数,但有一些问题,它没有返回到ajax成功函数。
public function addlot()
{
$this->loadModel('lot');
$this->layout = 'ajax';
$this->autoRender = false;
$lotno=$this->request->data['name1'];
$loc = $this->Auth->user('location');
$acc=array('lot_no'=>$lotno,'location'=>$loc);
$this->lot->save($acc);
$this->loadModel('lot');
$this->lot->recursive=0;
$this->set('lots',$this->lot->find('all',array('conditions'=>array('location'=>$loc))));
echo 2;
}发布于 2018-06-28 19:04:15
首先,你需要改变
url: '<?php echo Router::url(array('controller'=>'Partconfs','action'=>'addlot'));?>',至
url: "<?php echo Router::url(array('controller'=>'Partconfs','action'=>'addlot'));?>", 为了调试你的ajax,你可以检查浏览器的“网络”标签,从那里选择你的ajax调用,并检查你得到的是什么错误。请找到附件中的图片以供参考

https://stackoverflow.com/questions/51076508
复制相似问题