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 had the same problem using asp.net MVC and found the solution here
Works like a charm.
CODE
To build on @felipe-miosso's answer:
Add it to your application:
You can set the default "Content-Type" like this:
About the
data
format:Try to use this variation
In my case I resolve the problem like this :
You need to use JSON.stringify for each param containing a JSON object, and then build your data object with "$.param" :-)
NB : My "objJSON" is a JSON object containing array, integer, string and html content. His total size is >3500 characters.
I solved this by below codes:
Client Side (Js):
notice that data is an object.
On the server (ASP.NET MVC):
and 'AllowCrossSiteJsonAttribute' is needed for cross domain requests:
Hope this was useful.
I don't have the reputation to comment, but in response/addition to Don F's answer:
$params = json_decode(file_get_contents('php://input'));
A second parameter of
true
needs to be added to thejson_decode
function in order to properly return an associative array:$params = json_decode(file_get_contents('php://input'), true);