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
I had the same problem using asp.net MVC and found the solution here
There is much confusion among newcomers to AngularJS as to why the
$http
service shorthand functions ($http.post()
, etc.) don’t appear to be swappable with the jQuery equivalents (jQuery.post()
, etc.)The difference is in how jQuery and AngularJS serialize and transmit the data. Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively ... By default, jQuery transmits data using
Content-Type: x-www-form-urlencoded
and the familiar
foo=bar&baz=moe
serialization.AngularJS, however, transmits data using
Content-Type: application/json
and
{ "foo": "bar", "baz": "moe" }
JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.
Works like a charm.
CODE
// Your app's root module...
angular.module('MyModule', [], function($httpProvider) {
// Use x-www-form-urlencoded Content-Type
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function(obj) {
var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
for(name in obj) {
value = obj[name];
if(value instanceof Array) {
for(i=0; i