Angular http发布到Laravel后端请求始终为空

问题描述:

我有一个问题,我不知道如何解决。 我使用AngularJS 1向我的后端(Laravel 5.1)发布帖子。 这个帖子是AngularJS成功的。Angular http发布到Laravel后端请求始终为空

在我的Laravel控制器中,我使用Request从AngulrJS接收发布的数据,但$ request-> all()总是空的,我不知道为什么。

我错过了我的发布请求中的内容吗?

LARAVEL ROUTE: 

Route::post('/signup','[email protected]'); 


LARAVEL CONTROLLER: 
<?php 


namespace App\Http\Controllers; 
use Illuminate\Http\Request; 

class SignupController extends Controller 
{ 
    public function Signup(Request $request){ 


     dd($request->all()); <-- is always empty 
    } 
} 

ANGULARJS POST:

.controller('SignupCtrl',function($scope,$http,$state){ 

    $scope.Signup = function($params){ 

     $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 
     $http.post('http://localhost:8888/vemhamtar/public/signup',{"name":$params.name,"phone":$params.phone,"email":$params.email,"password":$params.password}) 
     .success(function(response){ 

      $params.name = ""; 
      $params.phone = ""; 
      $params.email = ""; 
      $params.password = ""; 
      $state.go("app.contacts"); 

     }) 
     .error(function(error){ 
      console.log(error); 
     }); 
    }; 
}) 

尝试使用$httpParamSerializer格式化你的有效载荷以URL编码格式的数据。

.controller('SignupCtrl',function($scope,$http,$state,$httpParamSerializer){ 

    $scope.Signup = function($params){ 

     $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 
     $http.post('http://localhost:8888/vemhamtar/public/signup',$httpParamSerializer({"name":$params.name,"phone":$params.phone,"email":$params.email,"password":$params.password})) 
     .success(function(response){ 

      $params.name = ""; 
      $params.phone = ""; 
      $params.email = ""; 
      $params.password = ""; 
      $state.go("app.contacts"); 

     }) 
     .error(function(error){ 
      console.log(error); 
     }); 
    }; 
}) 
+0

Awsome !!非常感谢! – Webbie