繁体   English   中英

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

[英]AngularJS $http.post call returning 502 (bad gateway) error

我正在开发网站,我在后端使用AngularJS,在后端使用Laravel php框架。

我有一个简单的表单,即发送$ http.post调用laravel后端,并处理数据并发送电子邮件。

在我的本地Apache服务器上一切正常,但在webhosting服务器上联机,它在开发者控制台中返回502(错误的网关错误)(见下面的截图)。

截图: http//s32.postimg.org/bwnxyps1x/502.jpg

但是,当出现此错误时,将执行后端脚本并发送电子邮件。 这真的很奇怪。

在AngularJS中,我有简单的工厂发送$ http.post调用...

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;

这是我的控制器。

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;

最后这只是laravel控制器中的简单方法。

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

    });
}

我不是AngularJS的专家,我对这个错误感到很困惑。 你有什么建议 - 可能是什么原因?

非常感谢你! 所有提示都表示赞赏。

502表示服务器无法正确完成某些操作。 所以它发送了一封电子邮件,但也许它会尝试在该脚本之后更新数据库。 由于您的本地工作正常并且您的遥控器工作正常,这归结为服务器问题,是否有可能在此服务器上有更多日志记录可以告诉我们为什么会发布502。 尝试发送电子邮件后,电子邮件程序可能会返回此信息吗?

看起来就好像后端不会向你的角度应用程序发回任何东西。 它应该以OK(例如200)HTTP状态响应。

好的。 所以我联系了虚拟主机提供商,他们在htaccess中关闭了OPcache,现在正在运行...... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM