0

I have question of both logic and implementation for which so far googling netted me nothing of use.

  • I need to access a webservice that can provide me 1,000 results at a time queries can return roughly 21,000 results
  • I have no control over the webservice, just need to use it
  • JS runs on a local machine or another webserver (ie NOT the server with the data)

So I wrote a small js script that calls it and parses the data, now my problem is that I written it using synchronous call (ie async: false). This presents a bunch of issues some of which mean that it only seems to work on chrome and not other browsers.


So my questions are:

How do you handle logic of making an asynchrnous call and parsing the results? - by the time I come out of the call, I have no results and they come in a bit later. How can I use them properly?

How can I do so using a webservice which I will need to poke a few times, for instance if I got a total of 10,000 results and the service returns only 1,000 at a time.

I am rather confused about these two aspects of the problem and would appreciate any help. I have tried to use ajax and cors, they both work to varying degree, but I do not understand how to handle the second part (only getting a subset of results at a time).

What methods are there for this given that originally I could not use async: true due to the "Access-Control-Allow-Origin". And I cannot modify the webservice.


Parsing a processing is already handled which I parse each individual xml reply to extract data I need and process it all at the later stage.

Would appreciate pseudo code for a reply, I am only starting out with learning JS so do not know all it is capable of doing.

1
  • As it stands, this is too broad. Try to limit yourself to one question per question. Commented Apr 9, 2015 at 9:12

1 Answer 1

1

You can use promises:

// calls your web service asynchronously   
function request(page) {    
    return $.ajax({
        url: '/xml-service/',
        method: 'POST',
        dataType: 'json',
        data: {
            delay: 1,
            json: JSON.stringify(ret)
        }
    });
}

// retrieve all results recursively
function requestItems(page, items) {
    return request(page).then(function(data){
        if (data.currentPage > data.totalPage) {
            return items;
        } else {
            return requestItems(data.currentPage + 1, items.concat(data.items));
        }
    });
}

function requestAll(){
    return requestItems(1, []);
}

requestAll().done(function(items) {
    console.dir(items);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cool, thanks for that, that actually did it only in the end I used a GET since it also retrieves the data but does not complain about the "access control allow origin"!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.