Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

Could anyone tell me why the following statement does not send the post data to the designated URL? The URL is called but on the server when I print $_POST - I get an empty array. If I print the message in the console before adding it to the data - it shows the correct content.

$http.post('request-url', { 'message' : message });

I've also tried it with the data as a string (with the same outcome):

$http.post('request-url', "message=" + message);

It seems to be working when I use it in the following format:

$http({ 

method: 'POST', 

url: 'request-url', 

data: "message=" + message, 

headers: {'Content-Type': 'application/x-www-form-urlencoded'} 

});

but is there a way of doing it with the $http.post() - and do I always have to include the header in order for it to work? I believe that the above content type is specifying the format of the sent data, but can I send it as a javascript object?

1 Answer

0 votes
by (106k points)

If you are receiving the request in PHP you can use:

$params = json_decode(file_get_contents('php://input'),true);

To access an array in PHP from an AngularJS POST.

Related questions

+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 30, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...