Hi, How is the best way to write an app.request (get/post) reusable?
In my app I have a lot of request (get/post) and for each task I write something like this…
//GET
var url = endpoint + 'users/';
var query = '{"id": "20"}';
app.request.get(url,
{keys: key,
where: query,
username: username,
lastname: lastname,
data: data
},
function (data) {
data = JSON.parse(data);
….code....
}
//POST
var url = endpoint + 'users/';
app.request.post(url,
{keys: key,
username: username,
lastname: lastname,
data: data
},
function (data) {
data = JSON.parse(data);
switch(d.response.code)
{
case '400': ...code.. break;
case '200': ...code… break;
}
}
The two things that changing in my request is the type (get or post) and the params(sometimes I have all the params above and sometimes only two)… I’d like to create only one function, but I don’t know how to do it. Sorry for my question, I’m a beginner. Thank you in advance for your help.
app.request.setup(parameters)- Set default values for future Ajax requests.
Its use is not recommended
parameters - object - Default requests parameters