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: