簡體   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