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

时间:2017-01-09 21:40:49

标签: angularjs ajax laravel-5 laravel-5.1

我有一个问题,我不知道如何解决。 我使用AngularJS 1向我的后端发帖(Laravel 5.1)。 这篇文章来自AngularJS。

在我的Laravel控制器中,我使用Request来接收来自AngulrJS的发布数据,但$ request-> all()始终为空,我不知道原因。

我在帖子请求中遗漏了什么?

LARAVEL ROUTE:

Route::post('/signup','SignupController@Signup');


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 个答案:

答案 0 :(得分: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);
        });
    };
})