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 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 string (with the same outcome):
$http.post('request-url', "message=" + message);
It seem 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 format of the sent data, but can I send it as javascript object?
It's not super clear above, but 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.
Just updated from angular 1.2 to 1.3, have found a problem in the code. Transforming a resource will lead to an endless-loop because (I think) of the $promise holding again the same object. Maybe it will help someone...
I could fix that by:
I like to use a function to convert objects to post params.
To send data via Post methode with
$http
of angularjs you need to changedata: "message=" + message
, withdata: $.param({message:message})
If your using PHP this is a easy way to access an array in PHP from an AngularJS POST.
this is probably a late answer but i think the most proper way is to use the same piece of code angular use when doing a "get" request using you
$httpParamSerializer
will have to inject it to your controller so you can simply do the following without having to use Jquery at all ,$http.post(url,$httpParamSerializer({param:val}))