2017-01-09 53 views
1

我有一個問題,我不知道如何解決。 我使用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); 
     }); 
    }; 
}) 

回答

1

嘗試使用$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