Wednesday, 11 September 2013

How to write a small helper function for Ajax Post requests?

How to write a small helper function for Ajax Post requests?

such that an object literal contains the parameters you want to send and
the result is a string you can send as data.
Here is my first try:
function makeData(obj) {
var data_send = '';
_.each(obj, function (val, key) {
data_send += encodeURIComponent(key) + '=' +
encodeURIComponent(val) + '&';
});
data_send.slice(1,-1);
return data_send;
}
Is this the correct form for ajax post data?

No comments:

Post a Comment