Pass array as parameter for web api in php with Angularjs

Asked

Viewed 720 times

0

In my local environment I use PHP7 and developed Restful API for an application that uses Angujarjs. I needed to make a get request to the Api and pass an array as parameter and did so

$http.get("/Api/MinhaUrl.php", {
    params: {
        "Nomes[]": ["João", "Maria"]
    }
})
.then(function(response){ //dispara ao realizar requisição com sucesso
    console.log(response.data);
}, function(response){ //dispara ao falhar
    console.log(response.statusText);         
});

In my Minhaurl.php file I printed the parameters on the screen as follows:

<?php
   print_r($_GET);
?> 

When executed was printed in the browser console exactly what I expected, so:

Array
(
    [Nomes] => Array
        (
            [0] => João
            [1] => Maria
        )

)

So far no problem. However, when I upload this application to the server, which only supports version 5.6 of PHP (I don’t know if this is related to the problem but I believe so), the parameter with the array is received by PHP otherwise, and what is printed in the browser console is as follows:

Array
(
    [Nomes%5B%5D] => Maria
)

It "understands" the signals of [] by their HTML code %5B and %5D, takes only the last element of the array and interprets it as a common variable.

What should I do to read the parameters on the server in the same way I read in the local environment?

1 answer

0

$http(
  method: 'GET',
  url: '/items',
  params: {
    id: JSON.stringify(ids) // ids is [1, 2, 3, 4]
  }
)

It’s there, the way the variable ids is an array. I hope to have helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.