How can I serialize an object without using the $.param
jquery?
I want the object below:
var user = {username: 'ronald.araujo', password: '123456',};
to have the following output:
username=ronald.araujo&password=123456
Any suggestions? Remembering that I would do this using Angularjs or pure Javascript.
EDIT:
I am using the verb "save" ($resource) the angularjs. How could I set the header "application / x-www-form-urlencoded" and serialize?
Pure javascript can do it just fine:
function serializeObj(obj) {
var result = [];
for (var property in obj)
result.push(encodeURIComponent(property) + "=" + encodeURIComponent(obj[property]));
return result.join("&");
}
var user = {
username: 'ronald.araujo',
password: '123456'
};
var serialized = serializeObj(user);
console.log(serialized); //username=ronald.araujo&password=123456
The link to original answer: How do I POST urlencoded form data with $http in AngularJS?
You can use $httpParamSerializerJQLike
;
.controller(function($http, $httpParamSerializerJQLike) {
//...
var serializedUser = $httpParamSerializerJQLike(user);
});
you can see $httpParamSerializerJQLike
Documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With