수색…


기본 CakePHP 2.x 예제

컨트롤러 : 컨트롤러에서는 RequestHandler 구성 요소를 추가해야합니다. 이렇게하면 CakePHP가 Ajax 요청을 자동으로 감지 할 수 있습니다 (자세한 내용은 http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html 참조).

class YourController extends AppController {
    public $components = array('RequestHandler');
    //...

    public function ajaxCall() {
        if($this->request->is('ajax'){
            // some code that should be executed
            // ...
            // variables you want to return
            $this->set(compact('firstVariable', 'secondVariable'));
            $this->set('_serialize', array('firstVariable', secondVariable))
    }
}

코드보기 (jQuery 사용) :

<script>
$.ajax({
    type: 'POST',
    url: '/yourController/ajaxCall',
    success: function (result) {
        // result is a JSON array of the returned Variables
    },
    error: function (result){
        console.log(result);
    }
});
</script>

Cakephp 2.x의 Ajax 요청

cakephp에서 ajax를 사용하는 또 하나의 방법. Cakephp는 Ajax 요청을 제공한다. 예제를 참조하십시오.

 $data = $this->Js->get('#id')->serializeForm(array('isForm' => true, 'inline' => true));
//on click send request to controller and displays response data in view
$this->Js->get('#button-id')->event(
        'click', $this->Js->request(
                array('controller' => 'Users', 'action' => 'add'), array(
            'update' => '#time', // field you wish to update
            'data' => $data, // Form Data in serialize form
            'async' => true,    
            'dataExpression'=>true,
            'method' => 'POST' // method get or post
                )
        )
);


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow