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

标签 javascript php angularjs forms

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

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

在我的本地 Apache 服务器上一切正常,但在网络托管服务器上在线时,它在开发人员控制台中返回 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;

这是我的 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;

最后,这只是 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);

    });
}

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

非常感谢!感谢所有提示。

最佳答案

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

关于javascript - AngularJS $http.post 调用返回 502(坏网关)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050004/

相关文章:

javascript - 如果选中单选按钮更改父 div (li) 背景 - javascript

php - 在 Windows 10 上安装 Apache 2.4 和 PHP 7

javascript - Angular : dynamic stylesheet link tag fires request too soon

javascript - 无法将参数传递给 angularjs 指令

javascript - 在不中断调整大小的情况下重置 jquery.resizable 元素的 "style"属性?

javascript - jquery 如果输入包含来自 array-child friendly global chat 的短语

php - 检索和保存数据

php - 在 PHP 中将年份与日期 ('Y-m-d' 进行比较)

javascript - ng-click 中复选框返回 false 而不是 true

javascript - 为 d3 colorScale 添加不透明度