getPost() Method doesn't works, & POST a form data with Ajax

Hello, i have problem with getPost() method.

I want to post a form data with POST Ajax and calling a Controller

but the Controller didn't get the input value

Form HTML

<div class="modal fade" id="modalTambah" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Tambah Data Mahasiswa</h4>
      </div>
      <div class="modal-body">
      <!--<form action="{{url("option/add")}}" class="form-horizontal" method="POST">-->
      <form class="form-horizontal">
        <div class="form-group">
          <label class="control-label col-sm-2" for="txtnim">NIM:</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" name="txtnim" id="txtnim" placeholder="Masukkan NIM">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="txtname">Nama:</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" name="txtname" id="txtname" placeholder="Masukkan Nama">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="txtdepartment">Departemen:</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" name="txtdepartment" id="txtdepartment" placeholder="Masukkan Departemen">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button onclick="doSaves()" type="submit" class="btn btn-success">Add</button>
            <a data-dismiss="modal" class="btn btn-danger" role="button">Batal</a>
          </div>
        </div>
      </form>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>

JavaScripts

function doSaves(){
          var nim = $("#txtnim").val();
          var name = $("#txtname").val();
          var department = $("#txtdepartment").val();

          /*alert(nim);*/

          var form_data = {
            nim: nim,
            name: name,
            department: department
          };

          $.ajax({
            type: "POST",
            url: "{{ url('Option/add') }}",
            data: form_data,

            success: function(resData){
              alert("success: " + resData);
              /*var data =  jQuery.parseJSON(resData);
              alert("success: " + data);*/
            },
            error: function(e){
              alert("error " + e);
            }
          });
        };

Controller


    <?php
    use Phalcon\Http\Request;
    class OptionController extends \Phalcon\Mvc\Controller
    {
    public function addAction()
    {
    $mhs = new Mahasiswa();
    $request = new Request();
    $response = new \Phalcon\Http\Response();

    $this->view->disable();

    $mhs->nim = $request->getPost('txtnim');
    $mhs->name = $request->getPost('txtname');
    $mhs->department = $request->getPost('txtdepartment');

    $stringError = "";

    if(!$mhs->save())
    {
      foreach ($mhs->getMessages() as $message) {
        $stringError = $stringError . $message;
      }
      $response->setContent(json_encode($stringError));
    }
    else
    {
      $response->setContent(json_encode($mhs));
    }
    return $response;
    }
    }

i don't know what's wrong with this code

here's the result

result



331

Try to remove 'txt' prefix from controller (because in doSaves() you post this parameters also without this prefix).

$mhs->nim = $request->getPost('nim');
$mhs->name = $request->getPost('name');
$mhs->department = $request->getPost('department');

Try to remove 'txt' prefix from controller (because in doSaves() you post this parameters also without this prefix).

$mhs->nim = $request->getPost('nim');
$mhs->name = $request->getPost('name');
$mhs->department = $request->getPost('department');

thanks for your reply,

i have tried your solution and here's the result

result2

what makes me confusing, the data has been stored into database

but alert method gives error result



331
edited Jul '17

I would find the details of the error:

error: function(xhr, status, error) {
    alert(xhr.responseText);
}

(And check php error log.)