1
Hello
It is very common to see on the internet calls ajax blocks that always repeat exhaustively the same structure, I would like to improve my calls using parameterization patterns and save code of my processes.
Current pattern:
$.ajax({
type: "GET",
url: url,
dataType: "html",
timeout: 30000,
data:
{
//parametros
},
success: function(data)
{
//caso sucesso...
},
error: function (jqXHR, textStatus, errorThrown)
{
//caso erro
},
});
I would like to propose a role that is centralizing, something like chamarAjax("POST", url, array(parameters))
But that is where the doubts arise: 1) Whereas parameters can be an array (key/value) how to pass them considering they can be 'n' ?
2) How to work with the return of the executed function (Success, error, complete) in the process/script that called the function ?
I looked for examples of this paradigm but found nothing, I appreciate any indication
You can instead use ajax to use AXIOS. It reduces the code that is used a bit. https://github.com/axios/axios
– Lucas Brogni
Thanks @Lucasbrogni for the nomination, I will evaluate
– MCunha98
pq not using $.post and $.get?
– Tobias Mesquita
@Tobiasmesquita nothing against, the point is not themselves, but to concentrate calls. For example instead of having a $.get('user/blablabla') and all its configuration (with timeout for example) I wanted to have something simpler in hand
– MCunha98
You cannot put in a Function with all the parameters?
– Ricardo Pontual
@Ricardopunctual yes, that’s the idea ! But when I talk about parameters like date (or an array) and at the same time, like Interceptor Success/error/, Voce happens to have some example ?
– MCunha98
@DVD thanks for sharing, I will evaluate !
– MCunha98
was going to post something similar to @dvd posted, but his example is very good, taking advantage of the
done
to call the function and returning date, very good– Ricardo Pontual
@dvd was perfect what you sent, gave to explore well and generate for my pattern smoothly. Thanks again !
– MCunha98
Good... I will post as an answer because it gave me work rs..
– Sam