如何使用 $http_post AngularJS + Laravel 重定向

How to redirect with $http_post AngularJS + Laravel ?

本文关键字:AngularJS Laravel 重定向 post 何使用 http      更新时间:2023-09-26

今天早上我发现了$http_method,它太棒了!但是....我有一个问题另一个时间 XD

首先,我发送了正确的内容,并且DataBasae已正确更新,但是当我尝试使用以下代码更新站点时,我遇到了错误。

 $timeout(function() {
                $location.path('/');
              });

我的代码是这样的

$scope.processForm = function() {
                $http({
                    method  : 'POST',
                    url     : 'todos',
                    data    : $.param($scope.formData),  // pass in data as strings
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
                })                
            };

我的文件路由中的控制器.php在 Laravel 中是下一个:

Route::post('todos', function()
    {
        $user = new Nodes;
        $user->name = Input::get("name");
        $user->save();
    });

当我在表单中发送内容时,我不知道如何更新网页......对此有什么解决方案吗?

在控制器中,将$location作为服务依赖项注入,然后添加一个 then 函数,该函数确定当服务器成功响应时会发生什么

app.controller('ctrlName', ['$scope', '$location', function($scope, $location){
    $scope.processForm = function() {
            $http({
                method  : 'POST',
                url     : 'todos',
                data    : $.param($scope.formData),  // pass in data as strings
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
            }).then(function(response){
                //successfully posted data
                $location.path('/');
            }, function(response){
                //error has occurred
            })                
        }; 
}])