-
Notifications
You must be signed in to change notification settings - Fork 27.4k
x-www-form-urlencoded and $resource #1937
Comments
I have used the following against master: angular.module('restServices', [ 'ngResource' ])
.factory('UserWs', function($resource) {
return $resource('/webBase/ws/user', {}, {
login : {
method : 'POST',
url : '/webBase/ws/user/login',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
})
}) And then in the controller: function LoginCtrl($scope, UserWs) {
$scope.user = null;
$scope.login = function() {
UserWs.login($.param({
username : $scope.username,
password : $scope.password,
})});
};
}; Note the use of $.param as the object will not automatically convert to a url encoded string (though I think it should). |
You can setup the $httpProvider of your app to have x-www-form-urlencoded as default content type for POST request. .config(function($httpProvider) {
$httpProvider.defaults.headers.post = {'Content-Type': 'application/x-www-form-urlencoded'};
}); |
I currently have the same issue. What really happens is the $.param operation will run through all properties of you object. Resource objects have $get, $delete and other properties as well, in which case the property is invoked and the result is being used as the parameter value: From their code: // If value is a function, invoke it and return its value
value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value ); I'm trying now to clone the data object, removing all properties starting with a $ and using that to pass to the $.param function. I'll let you know if it works. |
Ok, this seems to work: Use the following code for your transformRequest: $httpProvider.defaults.transformRequest = function(data) {
if (data === undefined) return data;
var clonedData = clone(data);
for (var property in clonedData)
if (property.substr(0, 1) == '$')
clonedData[property] = null;
return $.param(clonedData);
}; |
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months. Please try the newest versions of Angular ( Thanks! |
As of Angular 1.2.5, this is still an issue. To add onto calmera's response, I found using "delete" on the property to be the most effective:
|
Thanks @far-out Following code worked well for me- In app config -
With your resource request -
|
@NKjoep Angular allows to set transformRequest and contentType header for a specific request. Just create your own action in $resourse declaration.
|
Thx!!!! @BrynCooke your sample works perfect 👍 |
Thanks @vivex , It worked for me too |
My backend is PHP and I followed this guide
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
This allows me to use $http.post to sent POST data to my server.
Without doing this PHP cannot even see any POST data since it has no idea how to parse a JSON object.
$http.post works fine as in the example below
What I am having issues with is with $resource.
After following that guide and trying to run the following code
When I run the save function, it freaks out and tries to make many requests: GET, POST, GET, DELETE, DELETE, POST
It should be making a single POST request. It seems like it is running through many of the possible functions of a $resource object.
Has anyone had success using a $resource to send POST data to a PHP backend or am I missing something here?
The text was updated successfully, but these errors were encountered: