title: zend framework 2 ajax request error
body: <p>Im learning Zend framework 2. I found a post on how to make a Jquery modal window ajax request in zend. I made a little popup window with it where a user can add an album name and also an album type.
  When I'm running the scripts below <strong>without</strong> the Jquery modal window it saves perfectly to my database, but when I'm trying to do that via ajax nothing getting into the database. I started to look for the error and i found in the console when i want to post the form:</p>
  
  <blockquote>
    <p>Failed to load resource: the server responded with a status of 500 (Internal Server Error) <code>http://localhost/album/validatepostajax</code></p>
  </blockquote>
  
  <p>but when I'm clicking on the URL it loads.</p>
  
  <p>Any help is greatly appreciated. </p>
  
  <p>Controller:</p>
  
  <pre><code>&lt;?php 
  namespace Album\Controller;
  
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\View\Model\ViewModel;
  
  class AddNewAlbumController extends AbstractActionController {
  
  public function savetodb($data)
  {
      $mapper = $this-&gt;getServiceLocator()-&gt;get('Album/Mapper/Album');
      $mapper-&gt;insert($data);
  }
  protected function getForm()
  {
  
      $form = $this-&gt;getServiceLocator()-&gt;get('Album\Form\AddNewAlbumForm');
      $form-&gt;setInputFilter(new \Album\Form\Filters\AddNewAlbumFormFilter());
      $form-&gt;setName('AlbumForm');
  
      return $form;
  }
  
  public function newalbumAction()
  {
      $viewmodel = new ViewModel();
      $viewmodel-&gt;setTemplate('album/form/add-album.phtml');
      $form       = $this-&gt;getForm();
      $form-&gt;setHydrator ( new \Zend\Stdlib\Hydrator\Reflection () );
  
      $request = $this-&gt;getRequest();
  
      //disable layout if request by Ajax
      $viewmodel-&gt;setTerminal($request-&gt;isXmlHttpRequest());
  
      $is_xmlhttprequest = 1;
      if ( ! $request-&gt;isXmlHttpRequest()){
          //if NOT using Ajax
          $is_xmlhttprequest = 0;
          if ($request-&gt;isPost()){
              $form-&gt;bind ( new \Album\Entity\Album());
              $form-&gt;setData($request-&gt;getPost());
              $mapper = $this-&gt;getServiceLocator()-&gt;get('Album/Mapper/Album');
              if ($form-&gt;isValid()){
                  $this-&gt;savetodb($form-&gt;getData());
              }
          }
      }
  
      $viewmodel-&gt;setVariables(array(
                  'form' =&gt; $form,
                  'is_xmlhttprequest' =&gt; $is_xmlhttprequest //need for check this form is in modal dialog or not in view
      ));
  
      return $viewmodel;
  }
  
  public function validatepostajaxAction()
  {
      $form    = $this-&gt;getForm();
      $form-&gt;setHydrator ( new \Zend\Stdlib\Hydrator\Reflection () );
      $request = $this-&gt;getRequest();
      $response = $this-&gt;getResponse();
  
      $messages = array();
      if ($request-&gt;isPost()){
          $form-&gt;bind ( new \Album\Entity\Album());
          $form-&gt;setData($request-&gt;getPost());
  
          if ( ! $form-&gt;isValid()) {
              $errors = $form-&gt;getMessages();
              foreach($errors as $key=&gt;$row)
              {
                  if (!empty($row) &amp;&amp; $key != 'submit') {
                      foreach($row as $keyer =&gt; $rower)
                      {
                          $messages[$key][] = $rower;
                      }
                  }
              }
          }
  
          if (!empty($messages)){
              $response-&gt;setContent(\Zend\Json\Json::encode($messages));
          } else {
              $this-&gt;savetodb($form-&gt;getData());
              $response-&gt;setContent(\Zend\Json\Json::encode(array('success'=&gt;1)));
          }
      }
  
      return $response;
  }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  var is_xmlhttprequest = &lt;?php echo $this-&gt;is_xmlhttprequest; ?&gt;;
  var urlform           = '&lt;?php echo $this-&gt;url('album\newalbum',
           array( 'action' =&gt; 'validatepostajax'));?&gt;';
  &lt;/script&gt;
  
  &lt;?php echo $this-&gt;headScript()-&gt;appendFile($this-&gt;basePath() . '/js/ajaxform-up.js'); ?&gt;
  
  &lt;?php
  $form = $this-&gt;form;
  $form-&gt;setAttribute('action', $this-&gt;url('album\newalbum',
           array( 'action' =&gt; 'newalbum'))
  );
  $form-&gt;prepare();
  ?&gt;
  
  &lt;?php 
  echo $this-&gt;form()-&gt;openTag($form); 
  ?&gt;
  &lt;div class="element element_name"&gt;
  &lt;?php echo  $this-&gt;formlabel($form-&gt;get('name')).  $this-&gt;formelement($form-&gt;get('name'));?&gt;
  &lt;/div&gt;
  &lt;div class="element element_type"&gt;
  &lt;?php echo  $this-&gt;formElement($form-&gt;get('type'));?&gt;
  &lt;/div&gt;
  &lt;?php echo $this-&gt;formElement($form-&gt;get('submit')).$this-&gt;form()-&gt;closeTag(); ?&gt;
  </code></pre>
  
  <p>Js:</p>
  
  <pre><code>$(function(){
  $("form#AlbumForm").submit(function(){
  
      if (is_xmlhttprequest == 0)
          return true;
  
      $.post(urlform, { 'name' : $('input[name=name]').val(),
          'type' : $('select[name=type]').val()}, function(itemJson){
  
              var error = false;
  
              if (itemJson.name != undefined){
  
                  $(".element_name").append("&lt;div class = 'alert alert-error'&gt;"+itemJson.name[0]+"&lt;/div&gt;");
  
                  error = true;
  
              }
  
              if (itemJson.type != undefined){
  
                  $(".element_type").append("&lt;div class = 'alert alert-error'&gt;"+itemJson.type[0]+"&lt;/div&gt;");
  
                  error = true;
  
              }
  
              $( "winpopup" ).dialog( "option", "position",  { my: "center", at: "center", of: window } );
              if (!error){
                  $("#winpopup").dialog('close');
                  location.reload();
                  if (itemJson.success == 1){
                      alert('Data saved');   
                  }
              }
  
      }, 'json');
  
      return false;
  });
  });    
  </code></pre>