1

I want to send an array of objects to a php controller, and im doing like this:

$http({
        method: 'POST',
        url: "payment/pagamento",
        data: $.param(vm.listProdsCar),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(response) {
            console.log(response);   

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

My array vm.listProdsCar: array

and in the php controller im reeiving like this but is not working:

foreach($request->all as $req){

 echo '<script type="text/javascript">alert("'.$req->name.'");</script>'; }

Any ideas?

  • It would be wiser to send the data as JSON instead of urlencoded. Also the [`.success` and `,error` methods are deprecated and removed from AngularJS 1.6](https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Feb 06 '18 at 19:03
  • Read [Posting JSON To Laravel](https://stackoverflow.com/a/33298205/5535245). – georgeawg Feb 06 '18 at 19:05

1 Answers1

1

PHP isn't going to construct an STD Class from your javascript input, it's going to convert them to associative arrays, so you should access them as an associative array:

$req['name'];

Come to think of it, you also don't need to define the Content-Type header as what you've got is already the default.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110