marifemac marifemac - 5 months ago 32
Javascript Question

How can I convert javascript json data to be x-www-form-urlencoded?

I need to send data to my server with a content type of this:

'Content-Type': 'application/x-www-form-urlencoded'


Here's the data that I have from my AngularJS client:

user.loginData = {
userName: "joe@joe.com",
password: "smith++"
};


Can someone help by telling me how I can transform the userName and password so I can
send this data in a urlencoded form to my server:

data: 'grant_type=password&username=' + username + '&password=' + password,

Answer

I found a very simple solution:

encodeURIComponent(userName)