1

I have a problem that i don't know how to solve. I use AngularJS 1 to make a post to my backend (Laravel 5.1). The post is successful from AngularJS.

In my Laravel controller i use Request to recieve the posted data from AngulrJS but the $request->all() is always empty and i dont know why.

I have i missed something in my post request?

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 Answer 1

1

Try using $httpParamSerializer to format your payload as url-encoded form data.

.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);
        });
    };
})
Sign up to request clarification or add additional context in comments.

1 Comment

Awsome!! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.