AngularJS $ http.post调用返回502(错误网关)错误

I am developing websites where I am using AngularJS in frontend and Laravel php framework in backend.

I have one simple form that is sending $http.post call to laravel backend and there are data processed and email is sended.

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).

Screenshot: http://s32.postimg.org/bwnxyps1x/502.jpg

And yet, when this error appears, script in backend is executed and email is sended. It is really weird.

In AngularJS I have simple factory that is sending $http.post call...

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;

This is my controller.

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) && (!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;

And finally this is just simple method in laravel controller.

public function sendForm(Request $request) {

    $v = Validator::make($request->all(), [
        'jmeno' => 'required'
    ]);


    if($v->passes()) {

        $input = $request->all();

        $this->sendMessage($input);

        return response()->json(['success' => true, 'message' => 'Formulář byl odeslán. Jakmile to bude možné, budete kontaktováni.']);
    }
    elseif($v->fails())
    {
        $input = $request->all();
        return response()->json(['success' => false,  'message' => 'Musíte doplnit povinné údaje']);
    }
}

     private function sendMessage($input)
{
    Mail::send('emails.formular', $input, function($message)
    {
        $message->from(self::FROM, self::SUBJECT);

        $message->to(self::TO);

    });
}

I am not expert on AngularJS and I am really confused by this error. Do you have any suggestions – what can be the cause?

Thank you very much! All tips are appreciated.

502 indicates the server could not complete something properly. So it is sending out an e-mail, but maybe it tries to something after that script like update a DB. As your local is working OK and your remote is working partially OK this boils down to some server issue, is it possible there is more logging on this server that can tell us why the 502 is being issued. Perhaps the e-mail program returns this after attempting to send the e-mail?

It seems just like backend doesn't send anything back to your angular app. It should response with OK (200 for example) HTTP status.

Okay. So I have contacted webhosting provider and they turned off OPcache in htaccess and it is working now... :)