title: AngularJS $http.post call returning 502 (bad gateway) error
body: <p>I am developing websites where I am using AngularJS in frontend and Laravel php framework in backend. </p>
  
  <p>I have one simple form that is sending $http.post call to laravel backend and there are data processed and email is sended. </p>
  
  <p>Everything works fine on my local Apache server but on online on webhosting server it returns 502 (bad gateway error) in developer console (see screenshot below).</p>
  
  <p>Screenshot: <a href="http://s32.postimg.org/bwnxyps1x/502.jpg" rel="nofollow">http://s32.postimg.org/bwnxyps1x/502.jpg</a> </p>
  
  <p>And yet, when this error appears, script in backend is executed and email is sended. It is really weird.  </p>
  
  <p>In AngularJS I have simple factory that is sending $http.post call...</p>
  
  <pre><code>var sendForm = function sendForm($http) {
  return {
      sendForm: function($scope) {
         return $http({
              method: 'POST',
              url: '/odeslat',
              data: {titul: $scope.form.titul, jmeno: $scope.form.jmeno, prijmeni: $scope.form.prijmeni, email: $scope.form.email,
                  telefon: $scope.form.telefon, zprava: $scope.form.zprava, human: $scope.form.human}
          });
      }
  }
  };
  
  module.exports = sendForm;
  </code></pre>
  
  <p>This is my controller. </p>
  
  <pre><code>var formularCtrl = function formularCtrl($scope, sendForm) {
  $scope.form = {};
  $scope.form.human = 'Jsem člověk.';
  var odeslano = false;
  
  $scope.submitForm = function submitForm(isValid) {
  $scope.showErrorMessage = false;
  $scope.errorMessage = "";
  if((isValid) &amp;&amp; (!odeslano))
  {
      sendForm.sendForm($scope).success(function(data){
          if (!data.success) {
              // if not successful, bind errors to error variables
              $scope.errorMessage = data.message;
              $scope.showErrorMessage = true;
  
          } else {
              // if successful, bind success message to message
              $scope.message = data.message;
              $scope.showMessage = true;
              odeslano = true;
          }
      });
  }
      else
  {
      $scope.showErrorMessage = true;
      $scope.errorMessage = "Doplňte prosím všechny povinné údaje. ";
  }
  
  }
  };
  
  module.exports = formularCtrl;
  </code></pre>
  
  <p>And finally this is just simple method in laravel controller. </p>
  
  <pre><code>public function sendForm(Request $request) {
  
      $v = Validator::make($request-&gt;all(), [
          'jmeno' =&gt; 'required'
      ]);
  
  
      if($v-&gt;passes()) {
  
          $input = $request-&gt;all();
  
          $this-&gt;sendMessage($input);
  
          return response()-&gt;json(['success' =&gt; true, 'message' =&gt; 'Formulář byl odeslán. Jakmile to bude možné, budete kontaktováni.']);
      }
      elseif($v-&gt;fails())
      {
          $input = $request-&gt;all();
          return response()-&gt;json(['success' =&gt; false,  'message' =&gt; 'Musíte doplnit povinné údaje']);
      }
  }
  
       private function sendMessage($input)
  {
      Mail::send('emails.formular', $input, function($message)
      {
          $message-&gt;from(self::FROM, self::SUBJECT);
  
          $message-&gt;to(self::TO);
  
      });
  }
  </code></pre>
  
  <p>I am not expert on AngularJS and I am really confused by this error. Do you have any suggestions – what can be the cause? </p>
  
  <p>Thank you very much! All tips are appreciated.</p>