Ajax函数无法正确执行

时间:2018-06-28 06:57:44

标签: javascript php jquery cakephp

这是我的.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保存在数据库的lot表中并回显2.因此它应该返回到ajax的成功函数,但是存在一些问题并且它没有返回到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;
              }

1 个答案:

答案 0 :(得分:0)

首先您需要更改

url: '<?php echo Router::url(array('controller'=>'Partconfs','action'=>'addlot'));?>',

url: "<?php echo Router::url(array('controller'=>'Partconfs','action'=>'addlot'));?>", 

并且为了调试您的ajax,您可以检查浏览器的“网络” 标签,然后从此处选择您的ajax调用,然后检查您遇到的错误。请找到所附图片以供参考

enter image description here