【问题标题】:Angular http post to Laravel backend Request always emptyAngular http post到Laravel后端请求总是空的
【发布时间】:2017-05-24 06:30:33
【问题描述】:

我有一个问题,我不知道如何解决。 我使用 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 帖子:

.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);
        });
  };
})

【问题讨论】:

    标签: angularjs ajax laravel-5 laravel-5.1


    【解决方案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);
            });
        };
    })
    

    【讨论】:

    • 太棒了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 2021-12-10
    • 1970-01-01
    • 2017-07-31
    • 2019-12-15
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多