Does phalcon support ajax?

I search Document just find:

// Getting a request instance $request = new \Phalcon\Http\Request();

// Check whether the request was made with method POST if ($request->isPost() == true) { // Check whether the request was made with Ajax if ($request->isAjax() == true) { echo "Request was made using POST and AJAX"; } }



11.1k

a lot of ajax request to get json data, how can I improve performance

Yes AJAX is supported as you pointed out. You can use the Response object to create a response and return it back or echo it In your controller you can use something like this (if the response is ajax)

$payload     = array(1, 2, 3);
$status      = 200;
$description = 'OK';
$headers     = array();
$contentType = 'application/json';
$content     = json_encode($payload);

$response = new \Phalcon\Http\Response();

$response->setStatusCode($status, $description);
$response->setContentType($contentType, 'UTF-8');
$response->setContent($content);

// Set the additional headers
foreach ($headers as $key => $value) {
   $response->setHeader($key, $value);
}

$this->view->disable();

return $response;


11.1k

Thank you for your help

Is there a good reason that JSON couldn't be written with Volt? AJAX is really just HTTP, the server doesn't need to know the difference.

Marcus,

The fact is that you are free to send your request as you want, with volt or not. But if you want to send json or xml then it would be foolish to use Volt.

Hi guys, I have tried using what you did here but for some reason it doesn't work for me, on the javascript is not showing me any response back, could be that my javascript is wrong? i see that it apparently calls the Action but it doesn't come back with data unless I leave the view undisabled in which case it won't bring the data I have sent but only the general layout I already have.

This is my JS:

<script type="text/javascript">
$(document).ready(function() {
$('#fieldDept').change(function() {
    //alert("ingreso paso 2");
    $.ajax({
        url: "formulario/listUsers", 
        success: function(result){
            $("#udiv").html(result);
        }});
});
});</script>

And my Action I have tried both of this with no succes:

public function listUsersAction()
{
    $this->view->disable();
    $request =$this->request;
    if ($request->isPost()==true) {
        if ($request->isAjax() == true) {                
            header('Content-type:application/json;charset=utf-8');
            echo json_encode([1, 2, 3]);
        }
    }
}

And the other one: public function listUsersAction() { $this->view->disable(); $request =$this->request; if ($request->isPost()==true) { if ($request->isAjax() == true) {

            // Getting a response instance
            $response = new Response();

            $feed     = "<h1>respuesta</h1>";
            $content = json_encode($feed);
            $status = 202;
            $description = "OK";
            $contentType = "application/json";

            // Set the content of the response
            $response->setStatusCode($status, $description);
            $response->setContentType($contentType, 'UTF-8');
            $response->setContent($content);
            return $response;
        }
    }
}

What am i doing wrong?

Thanks in advance