weixin_33725239 2014-10-02 20:59 采纳率: 0%
浏览 31

在cakephp中没有来自ajax的数据

I am working in CakePHP to pass data via ajax. The data enter into the database successfully. This means the data pases from the view to the controller fine. However, it never populated the #target div. I have tried to even manually set the $data in the controller, but it still did not work. Here is my controller

<?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'];            
        }

    }

}

Here is my 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,  
            complete: function(data) {
            //alert("hi neal"+selectedValue);
                $("#target").append(data);
            }
        });
        });
    });
</script>

Here is my view div

<div id="target"></div>

What iam i doing wrong

  • 写回答

4条回答 默认 最新

  • weixin_33713503 2014-10-03 07:34
    关注

    Somehow your action has to echo something or your Ajax call fetches a blank page, which means no data. There are many ways to do that.

    The most classical is using a view:

    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'));
        }
    }
    

    index.ctp

    echo $message;
    

    But you could for instance also return a JSON response:

    GenreselectsController:

    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));
        }
    }
    

    Or you could also use JSON or XML views

    评论

报告相同问题?