Multiple requests with $ .ajax, synchronously

-1

I am making a system, and in it I need to send several data to the server, basically synchronize an array of objects on a server via json using javascript, until there is no problem, however the request is $ .ajax, which is asynchronous, and therefore, I can not control the data later, nor save the data at the end of the execution of all the requests.

I have done with a for, and found on the net, first that could use async: true, however, I also saw that this function was deprecated, so one should use promise or callback, but both could not implement, there are some example how can I do this?

Edited to clarify the problem

consistency, since it is an array of data that may or may not already be in the database, so I use the id (which the database provides) to find out if the object is registered, if there is an id in the object, p>

But returning to the main, what happens is that when sending a data to the server, the asynchronous call of $ .ajax, if put so that in each success the data is saved, this results in that in some cases, the result end ends up being information with and without the id. resulting in unexpected behavior. this is why ajax calls, because they are asynchronous, do not end in series, and in parallel being each one in its time.

It is not only information, if someone has a technique that solves the problem, I guarantee that several developers will be grateful, of course, I will be much more.

    
asked by anonymous 06.07.2017 / 18:56

1 answer

4

Synchronous Ajax (Sjax) is being discontinued by browsers (not by jQuery, this is "consequence") and will probably be removed, you need to learn how to use callback, Promise is optional.

In JavaScript we almost always use callback, until the click on an element that triggers an event is a callback.

  

$ .ajax, which is asynchronous, so I can not control the data afterwards, nor save the data at the end of the execution of all the requests.

No, you are misunderstanding, the request does not occur "together", it occurs in paralero, but is only delivered after readyState equals 4 , when it arrives at 4 the request is already finished , I think maybe you have not understood the difference between backend and front end, they are different layers, different locations.

  

I can not control the data later

I do not understand the "control", perhaps you want an HTML element popular, for example:

$.ajax({
    url: "pagina.php" //Sua página
}).done(function( data ) {
    $("query para o elemento html").html(data);
}).fail(function (erro) {
    console.log("Houve um erro na requisição", erro);
});

If it is to return a json jQuery is smart (usually it has to have Content-Type set) and then detects assuming something like:

[
    {"foo": "valor 1"}, { "bar": "valor 2" }
]

Ajax should look like this:

$.ajax({
    url: "pagina.php" //Sua página
}).done(function( data ) {
    var target = $("query para o elemento html");
    target.html(""); //Apaga o conteudo do "inner" atual

    for (var k in data) {
        //Adiciona os elementos
        $("<p></p>").html(k + ": " + data[v]);
    }
}).fail(function (erro) {
    console.log("Houve um erro na requisição", erro);
});

Documentation: link

    
06.07.2017 / 19:07