Created
February 10, 2015 14:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
k