duanpo8329 2016-05-05 11:58
浏览 247

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.

  • 写回答

3条回答 默认 最新

  • douyiqi9640 2016-05-05 12:07
    关注

    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?

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘