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?
I've been using the accepted answer's code (Felipe's code) for a while and it's been working great (thanks, Felipe!).
However, recently I discovered that it has issues with empty objects or arrays. For example, when submitting this object:
PHP doesn't seem to see B and C at all. It gets this:
A look at the actual request in Chrome shows this:
I wrote an alternative code snippet. It seems to work well with my use-cases but I haven't tested it extensively so use with caution.
I used TypeScript because I like strong typing but it would be easy to convert to pure JS:
It's less efficient than Felipe's code but I don't think it matters much since it should be immediate compared to the overall overhead of the HTTP request itself.
Now PHP shows:
As far as I know it's not possible to get PHP to recognize that B.a and C are empty arrays, but at least the keys appear, which is important when there's code that relies on the a certain structure even when its essentially empty inside.
Also note that it converts undefineds and nulls to empty strings.
I use jQuery param with AngularJS post requrest. Here is a example ... create AngularJS application module, where
myapp
is defined withng-app
in your HTML code.Now let us create a Login controller and POST email and password.
I don't like to exaplain the code, it is simple enough to understand :) Note that
param
is from jQuery, so you must install both jQuery and AngularJS to make it working. Here is a screenshot.Hope this is helpful. Thanks!
Add this in your js file:
and add this to your server file:
That should work.
This has finally been addressed in angular 1.4 using $httpParamSerializerJQLike
See https://github.com/angular/angular.js/issues/6039
It's not angular's fault. Angular is designed to work in JSON world. So when $http service send AJAX request, it send all your data as a payload, not as form-data so that your backend application can handle it. But jQuery does some things internally. You instruct jQuery's $ajax module to bind form-data as JSON but before sending AJAX request, it serialized JSON and add
application/x-www-form-urlencoded
header. This way your backend application able to received form-data in form of post parameters and not JSON.But you can modify angular $http service's default behavior by
$httpParamSerializerJQLike is angular's in-built service which serializes json in the same way $.param does of jQuery.
If you need a plugin to serialize form-data into JSON first, use this one https://github.com/marioizquierdo/jquery.serializeJSON
Unlike JQuery and for the sake of pedantry, Angular uses JSON format for POST data transfer from a client to the server (JQuery applies x-www-form-urlencoded presumably, although JQuery and Angular uses JSON for data imput). Therefore there are two parts of problem: in js client part and in your server part. So you need:
put js Angular client part like this:
AND
write in your server part to receive data from a client (if it is php).
Note: $_POST will not work!
The solution works for me fine, hopefully, and for you.