Skip to content

Instantly share code, notes, and snippets.

@kjaquier
Created February 10, 2015 14:39
Show Gist options
  • Save kjaquier/1888ecb4440c3bbd71a4 to your computer and use it in GitHub Desktop.
Save kjaquier/1888ecb4440c3bbd71a4 to your computer and use it in GitHub Desktop.
Fix for Angular's $http to use form encoding (instead of json) when sending POST/PUT requests. See http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data
angular.module('ngFixedHttp', [])
.factory('FixedHttp', ['$http', function($http) {
// Can't use $http.post() because JSON format is not
// recognized by the web server
// See : http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data
// This service is supposed to be used as a replacement for $http
// /!\ Not tested
// /!\ /!\ Doesn't work with nested JSON objects (must write custom compile function for that) /!\ /!\
var getFixedMethod = function(method) {
// didn't implement the 'config' argument for simplicity
return function(url, data) {
return $http({
method: method,
url: url,
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
};
};
return {
get: $http.get,
head: $http.head,
delete: $http.delete,
post: getFixedMethod('POST'),
put: getFixedMethod('PUT'),
patch: getFixedMethod('PATCH')
};
}])
@xiaoteai
Copy link

k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment