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 have had a similar issue, and I wonder if this can be useful as well: https://stackoverflow.com/a/11443066
Regards,
This code solved the issue for me. It is an application-level solution:
I know has accepted answer. But, following might help to future readers, if the answer doesn't suit them for any reason.
Angular doesn't do ajax same as jQuery. While I tried to follow the guide to modify angular
$httpprovider
, I encountered other problems. E.g. I use codeigniter in which$this->input->is_ajax_request()
function always failed (which was written by another programmer and used globally, so cant change) saying this was not real ajax request.To solve it, I took help of deferred promise. I tested it in Firefox, and ie9 and it worked.
I have following function defined outside any of the angular code. This function makes regular jquery ajax call and returns deferred/promise (I'm still learning) object.
Then I'm calling it angular code using the following code. Please note that we have to update the
$scope
manually using$scope.$apply()
.This may not be the perfect answer, but it allowed me to use jquery ajax calls with angular and allowed me to update the
$scope
.Hope it helps.
I had the same problem with AngularJS and Node.js + Express 4 + Router
Router expects the data from post's request in body. This body was always empty if i followed the example from Angular Docs
Notation 1
But if i used it in the data
Notation 2
Edit 1:
Otherwise node.js router will expect the data in req.body if used notation 1:
Which also sends the information as JSON payload. This is better in some cases where you have arrays in your json and x-www-form-urlencoded will give some problems.
it worked. Hope it helps.
Angular
WebAPI 2