Jquery cakephp中没有来自ajax的数据

Jquery cakephp中没有来自ajax的数据,jquery,ajax,cakephp,Jquery,Ajax,Cakephp,我在CakePHP中工作,通过ajax传递数据。数据已成功输入数据库。这意味着数据可以从视图传输到控制器。但是,它从未填充过#target div。我甚至尝试在控制器中手动设置$data,但它仍然不起作用。这是我的控制器 <?php App::uses('AppController', 'Controller'); class GenreselectsController extends AppController { public $components = array('R

我在CakePHP中工作,通过ajax传递数据。数据已成功输入数据库。这意味着数据可以从视图传输到控制器。但是,它从未填充过#target div。我甚至尝试在控制器中手动设置$data,但它仍然不起作用。这是我的控制器

<?php
App::uses('AppController', 'Controller');

class GenreselectsController extends AppController {

    public $components = array('RequestHandler');

    public $helpers = array('Js' => array('Jquery'));

    // single search function
    public function index() {
    $this->loadModel('Notification');
        if( $this->request->is('ajax') ) {
            $this->autoRender = false;              
            $this->Notification->create(); 
            $this->Notification->save($this->request->data);

            $data = $this->request->data['Notification']['message'];            
        }

    }

}

不知何故,您的操作必须回显某些内容,或者您的Ajax调用获取一个空白页面,这意味着没有数据。有很多方法可以做到这一点

最经典的方法是使用视图:

GenreselectsController:

public function index() {
    $this->loadModel('Notification');
    if( $this->request->is('ajax') ) {
        //$this->autoRender = false;
        $this->Notification->create(); 
        if($this->Notification->save($this->request->data)){
            $message = $this->request->data['Notification']['message'];
        }
        else{
            $message = 'something bad happened';
        }
        $this->set(compact('message'));
    }
}
public function index() {
    $this->loadModel('Notification');
    if( $this->request->is('ajax') ) {
        $this->autoRender = false;

        $result = array();

        $this->Notification->create(); 
        if($this->Notification->save($this->request->data)){
            $result['success'] = $this->request->data['Notification']['message'];
        }
        else{
            $result['error'] = 'something bad happened';
        }

        $this->response->type('json');
        $this->response->body(json_encode($result));
    }
}
index.ctp

echo $message;
但您也可以返回一个JSON响应:

GenreselectsController:

public function index() {
    $this->loadModel('Notification');
    if( $this->request->is('ajax') ) {
        //$this->autoRender = false;
        $this->Notification->create(); 
        if($this->Notification->save($this->request->data)){
            $message = $this->request->data['Notification']['message'];
        }
        else{
            $message = 'something bad happened';
        }
        $this->set(compact('message'));
    }
}
public function index() {
    $this->loadModel('Notification');
    if( $this->request->is('ajax') ) {
        $this->autoRender = false;

        $result = array();

        $this->Notification->create(); 
        if($this->Notification->save($this->request->data)){
            $result['success'] = $this->request->data['Notification']['message'];
        }
        else{
            $result['error'] = 'something bad happened';
        }

        $this->response->type('json');
        $this->response->body(json_encode($result));
    }
}

或者您也可以使用

您既没有回显/打印数据,也没有将其设置为任何视图(因为您已设置$this->autoRender=FALSE;),这就是为什么您没有将任何数据设置为目标

应该是这样的:

1) 如果将AutoRender设置为FALSE

<?php
App::uses('AppController', 'Controller');

class GenreselectsController extends AppController {

    public $components = array('RequestHandler');

    public $helpers = array('Js' => array('Jquery'));

    // single search function
    public function index() {
    $this->loadModel('Notification');
        if( $this->request->is('ajax') ) {
            $this->autoRender = false;              
            $this->Notification->create(); 
            $this->Notification->save($this->request->data);

            $data = $this->request->data['Notification']['message'];   
            echo $data;//echo data so that it is received by your ajax call
            exit();
        }

    }

}

明白了。我已经完成了ajax功能,而不是成功

<script>
    $(document).ready(function() {
        $('#genresearch').change(function() {           
            var selectedValue = $('#genresearch').val();

            var targeturl = '/genreselects/index/';

        $.ajax({        
            dataType: "html",
            type: "POST",           
            url: targeturl,
            async : true,
            data:{message:selectedValue},
            evalScripts: true,  
            success: function(data) {
            //alert("hi neal"+selectedValue);
                $("#target").append(data);
            }
        });
    });
});
</script>

$(文档).ready(函数(){
$('#genresearch').change(function(){
var selectedValue=$('#genresearch').val();
var targeturl='/genreselects/index/';
$.ajax({
数据类型:“html”,
类型:“POST”,
url:targeturl,
async:true,
数据:{消息:selectedValue},
evalscript:对,
成功:功能(数据){
//警报(“hi neal”+选定值);
$(“#目标”)。追加(数据);
}
});
});
});

您已设置$this->autoRender=false;因此,不会渲染任何视图 用这个代替

$this->render('sometemplatefile', 'ajax'); 
这将使用ajax布局

将此行添加到索引()中

创建sometemplatefile.ctp 然后把这个代码放进去

<?php echo $message?>


我看不到您的操作将数据返回到视图,您是否忘记使用
$this->set($data)设置数据?维尼,我有。我尝试了下面的方法,但它们不起作用$this->set('data',$data)$此->设置($data);您是否在浏览器控制台中检查了ajax调用是否返回了一些错误?很有趣。我该怎么做?我知道了。我试过了,没有错误或警告。没有什么。在我的控制台里。post、response和json都有数据。我很困惑在继续之前,您首先打印$this->request->data以查看所需的数据是否存在,并在执行save查询之前将其分配给变量。感谢各位的帮助
 $this->set('message', $data);
<?php echo $message?>