Several Ajax Requests

Asked

Viewed 490 times

0

I just did a routine to rescue information from Google Maps using your API. But I needed to use CURL in PHP to make the request, because when I do it directly in AJAX in JS, accuses me the error of Allow Origins Access Control and I could never solve it.

So I went to CURL. But regardless of this happens that there are several requests. I’m taking the distance from one address to the other by the Google API.

And it’s multiple on-screen records and accurate the distance of each one in real time, since I pick up the current location of the user.

So there could be more than 10 requests.

Is there any better way to do that ?

To understand I will post the code:

$.each(allEnderecos, function(i, v){
    var enderecoLocal = $(v).val();
    $.ajax({
        url: urlBase + '/select-endereco-local',
        data: { ea: enderecoAtual, el: enderecoLocal },
        dataType: "json",
        type: "POST",
        success: function(data){
            $(v).closest('div.info-dealer').find('span.geoval').html(data);
        }
    });
});

Look at the each. It is an unknown number of addresses you may have on screen. It depends on the user query.

1 - This is dangerous ?

2 - Interferes with the performance of the site ?

I thought without taking the information all at once and returning everything at once. But in PHP there is a CURL for another LINK, so I think it’s the same.

  • Will you do them in parallel ? needs a generic way of doing?

  • No, it’s serial. Like the code above. One after the other.

  • one depends on the other, hm, you know that ajax’s async false method is depreciated right? you can use the . Success or . done to execute dps that finish the request, take a look at this example: https://jsfiddle.net/gabrielr47/3da9aswq/4/

  • I do. But one does not depend on the other. These are different requests. See the code I posted. It’s a for. He seeks information and returns in span, then another, then another... different...

  • 1

    @Zoom, if the server supports compliance, I don’t see a problem with your approach. If you have a performance problem due to competition, you can try to implement a pool for your AJAX requests in the customer. an alternative would be to make your method possess an asyncrono return, something trivial using ASP.NET WebAPI, NodeJS/ExpressJS or SparkJava, but unfortunately the PHP does not yet support async/await, though I have reserved these words. But if this method becomes a bottleneck, because it’s a simple method, maybe it’s worth doing it in another technology.

  • 1

    @Zoom, another option for asynchronous requests would be to migrate part of your code from PHP for Hack, remembering that the latter is very similar to the PHP and is comparable with it, but supports the async.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.