phalcon dependebale dropdown

hy , excuse me i dont speak english very good :)

i created a dependble dropdown but i have erro in alert($.parsejson(data) return null controller action : $this->view->disable(); $response = new \Phalcon\Http\Response(); $request = new Phalcon\Http\Request(); $idessai= $request->get('idessai'); $query = "SELECT * FROM modalite WHERE modalite.idessai = $idessai"; $resultdis = $this->modelsManager->executeQuery($query); // vardump($resultdis); //return jsonencode($resultdis); return jsonencode($result_dis, true);

function ajax in my view is : <script> $("#idessai").change(function() { var value = $(this +'option:selected').val(); var getResultsUrl = '/STC4/prod/Query/moda'; $.ajax({ type: "GET", url: getResultsUrl, datatype: "json", data: {'idessai': value}, success: function(data){ data = $.parseJSON(data); alert(JSON.stringify(data, undefined, 2)); alert($.parseJSON(data)); =======> return null for(var i=0 ; i<data.lenght; i++){ console.log(data[i]); var chaine =''; for(var i = 0 ; i<data.lenght; i++){ chaine += '<option value="'+ data[i].idmod +'">'+ data[i].codemod +'</option>'; } $('#id_mod').html(chaine); } } }); }); </script> please if you have any idea in this subject help me

thanks

Please format your code :)



576

<script>

$("#id_essai").change(function() {

var value = $(this +'option:selected').val();

var getResultsUrl = '/STC4/prod/Query/moda';

$.ajax({
 type: "GET",
    url: getResultsUrl,
    datatype: "json",
    data: {'id_essai': value},
    success: function(data){
    data = $.parseJSON(data);
    alert(JSON.stringify(data, undefined, 2));
    alert($.parseJSON(data));  ===>>>> return  Null
    for(var i=0 ; i<data.lenght; i++){
    console.log(data[i]);
    var chaine ='';
    for(var i = 0 ; i<data.lenght; i++){
    chaine += '<option value="'+ data[i].id_mod +'">'+ data[i].code_mod +'</option>';
    }
    $('#id_mod').html(chaine);
  }
 }

}); }); </script> controlller Action ::

$this->view->disable();

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

           $request = new Phalcon\Http\Request();

           $id_essai= $request->get('id_essai');

           $query = "SELECT * FROM modalite WHERE modalite.id_essai = $id_essai";

           $result_dis = $this->modelsManager->executeQuery($query);

      //  var_dump($result_dis);

        //return json_encode($result_dis);

        return json_encode($result_dis, true);

        if you have an idea please help me
        thanks

first check if your route is defined as POST or GET, then if request return code 200 in your browser devtools.. in your json call, try like this:

    complete: function (response) {
            rep =response.responseTex);
            console.log( jQuery.parseJSON((rep.myKey));

if you plane to use a lot of json, implement something in your controllerBase.php:

    class ControllerBase extends Controller
        {
            public $jsonMessages = [];
            ...


            public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) 
            {
             if($this->request->isAjax() == true) {
                    $this->response->setContentType('application/json', 'UTF-8'); //--> maybe it's done by default withsetJsonContent()
                    $data = $dispatcher->getReturnedValue();
                    $this->response->setJsonContent($data);
                    return $this->response->send();
            }

then in your controller

        $this->jsonMessages['myKey'] = $my response
        return $this->jsonMessages;