PDA

View Full Version : howto multiple json requests without interference?


tomjoad2000
09-11-2009, 09:15 AM
I'm converting old js-libraries that does lookups at server to json (finally).

One of the issues I've had is that a new lookup intrerrupts any previous lookup that hasn't been completed. (It's all issued through an hidden iframe).

What's your suggestions regarding multiple requests with different destionations, like filling up different comboboxes etc.

Do I need to build a que-logic, or is there any mature cross-browser technique around?

Currently, JS sends request in hidden iframe, containing parameters and js-function that invoked the request. The server-response contains js-variable, and a call back to the original js-caller-function with "phase2";

fillCombo(lookupName,phase2) {
//if phase1, do request, sql is in session, attach "fillCombo" as caller...
//if phase2, invoked from loaded iframe containing data, fill combo.
}

As you can see, any new request to fill combo, before the prevoius has completed, will break the lookup.

A1ien51
09-11-2009, 01:07 PM
well if they need to be done in sequential order, you have to wait to the first one comes back. That means to break up the steps into more functions.

Eric

tomjoad2000
09-11-2009, 05:21 PM
Thanks for your reply,
however, there's no sequential relationship between the calls, basically it's just about:

User invokes json-req 1 from combo A.
Server doesn't reply fast enough
User invokes json-req 2 from combo B.
Answer for first request, back to combo A is lost.

How to solve it? With iframes, I'd need a que, or multiple iframes (sigh),
with JSON, can i put a request in a js-object?? Or jQuery as a wrapper?
What's the most easy and most supported solution??

ATB
/t

A1ien51
09-11-2009, 05:36 PM
Sounds like you need to build a solution that has its own iframes for each request and do not reuse the same iframe.

If you are using jQuery, why are you not using getJSON?

Eric

tomjoad2000
09-11-2009, 07:07 PM
Currently I'm not dependent of jQuery, and I prefer to go as light as possible, if there's a way to do it with standard ECMA js/json I'd rather go for that.

tomjoad2000
09-11-2009, 07:16 PM
Ah,
JSONRequest.get is what I'm looking for...

But that puts pretty much browsers in the attic according to wikipedia,
how to get older browsers on the track,
http://www.xucia.com/CrossSafe/readme.html ??

A1ien51
09-12-2009, 02:32 PM
You are going to have to use multiple iframes, or develop you own queuing system to use one iframe or use some sort of 3rd party library that already figured this stuff out.

Eric

rnd me
09-13-2009, 02:32 AM
i highly recommend jsonp for this.

a simple script-tag adder will let you make several calls at once.
it's also far simpler than messing with iframes, and it crosses domains to boot.

if you know the callbacks, you can build a que, or simply dispatch the next call in your callback, chaining them together...