0

There is a url where an API of orders is hosted. The API provides a method that delivers the headers of orders for a client and on a date. What it returns is a code of the order. There is a second method where, with the order code, it returns the detail of this order. Unfortunately the API does not have a method that brings me at once the header and detail of the order. I need to have the complete information of each order, its header and its detail.

Given the above, my strategy has been to make a small routine in jquery that first via Ajax invokes an API method that delivers all the headers of the orders, for a given date and a specific client. These headers store them in an array. After the above, I go through this arrangement and invoke another method of the API to obtain the respective details of the orders, which can be one or several records. The problem is that when executing, all the headers are obtained, but when the query of the details is executed, it is only able to recover the detail of the first order, because afterwards it throws an error message, the server responds with a message "Sorry, we have detected that there are simultaneous requests", without being able to obtain the details.

Apparently, the server keeps coming in parallel queries and that's why the server responds in that way. How could this recovery of the details of orders be improved, so that this error does not occur?

I do not know if it's a problem in the way I'm implementing or is it a problem with the jquery version?

<!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
        $("#btn_nuevo").click(function() {
          $.ajax({
            url: "http://api.mercadopublico.cl/servicios/v1/publico/ordenesdecompra.json?fecha=26062018&CodigoOrganismo=100448&ticket=C7A82D65-B7E3-44AD-8391-56DFD3DF067A",
            dataType: "json"
          }).then(data => {
            const total = data.Cantidad;
            $('#result').append('<b>Total: </b>' + total + '<br />');
            let promise = Promise.resolve();
            for (let j = 0; j < total; j++) {
              let url = "http://api.mercadopublico.cl/servicios/v1/publico/ordenesdecompra.json?codigo=" + data.Listado[j].Codigo + "&ticket=C7A82D65-B7E3-44AD-8391-56DFD3DF067A";
              $('#result').append('<b>NĀ°: </b>' + j + '<br />');
              $('#result').append('<b>Orden: </b>' + data.Listado[j].Codigo + '<br />');
              $('#result').append('<b>URL: </b>' + url + '<br />');
              $('#result').append('<b>-------------------------------------------------------------------------------------------------------------------- </br>');
              promise = promise.then(() => $.ajax({
                url: url,
                dataType: "json"
              })).then(datos => {
                $('#result').append('<b>Codigo: </b>' + datos.Listado[0].Codigo + '<br />');
                $('#result').append('<b>Nombre: </b>' + datos.Listado[0].Nombre + '<br />');
                $('#result').append('<b>Estado: </b>' + datos.Listado[0].Estado + '<br />');
                $('#result').append('<b>Descripcion: </b>' + datos.Listado[0].Descripcion + '<br />');
                $('#result').append('<b>Tipo: </b>' + datos.Listado[0].Tipo + '<br />');
              });
            }
          });
        });
      });

  </script>
</head>
<body>
    <div id="boton_nuevo" align="center">
      <input type="button" id="btn_nuevo" name="btnnuevo" value="Ejecutar">
    </div>
    <div id="result"></div>
</body>

When reviewing the states it throws, you see 3 states, which you see in the following images:

State 1

State 2

State 3

t.niese
  • 32,069
  • 7
  • 56
  • 86
Junco Fuerte
  • 125
  • 8
  • Your `promise = promise.then(...)` structure should serialize the requests. You can look in the network tab of the browser to verify that they are getting serialized as desired. Perhaps they are just too close together. Maybe you need a delay between them or maybe you're firing too many requests in too short a time (rate limiting)? ā€“ jfriend00 Jul 21 '18 at 16:21
  • Maybe it's a combination of both, many requests in a short time. Looking at the network tab, I see that the requests are coming out sequentially. If I assume that is this, how can I incorporate a delay or waiting time between one request and the next, because I do not care that there is a delay in the response of the details of the orders, what I need is to obtain the details of all the orders ā€“ Junco Fuerte Jul 21 '18 at 16:44
  • See [Using setTimeout on promise chain](https://stackoverflow.com/questions/39538473/using-settimeout-on-promise-chain/39538518#39538518) or [Delays between promises in promise chain](https://stackoverflow.com/questions/41079410/delays-between-promises-in-promise-chain/41079572#41079572). ā€“ jfriend00 Jul 21 '18 at 16:45

0 Answers0