Identifying XHR requests / responses?? Preventing

B

bizt

Hi,

I have a web app that makes requests to the server using
XmlHttpRequest object. I have created a js ajax wrapper function/class
(AjaxWrapper) that I can instantiate as an object and each object is
designed to make its own unique request (if I need to make two,
requests I would create two obejcts). Normally the object is created
within a function and will die at the end of that function call:

function doRequest() {
var oAjax = new AjaxWrapper;
oAjax.url = "data/getInfo.php";
oAjax.onSuccess = function() {
// handler code goes here
}
oAjax.get(); // this command makes the request
}

Problem:

I'm trying to identify a bug in which I suspect two requests that
share global variables (outside the caller function - doRequest in the
above example) are very occasionally being executed at the same time.
Now, I have to accept that this can happen as I have a feature that
automatically makes the requests without the users command but also
the user can make the same request at any time by other means. Anyway
I think that perhaps whilst one request is being made and another in
parallel at the same time when they are both being handled because
they share some variables one requests handling may be screwing up the
others.

Solution(?):

I decided that it should be a good idea to have some kinda of flag
that tells the system that this process is currently being handled, go
away. That way I wont have two requests being handled at the same
time. In fact, better yet may even be to have a flag to say that a
request is under way to prevent another call to the server even being
made. Good idea?
Now what I really could also do with, when I make a request it would
be neat if I could log the request ID if exists and when I get the
response I can check its request ID to see if it was the last request
made. Does XHR have this included or do I need to pass this when I
make my request and return it as part of my response generated. See
example:

Request

oAjax.url = "data/getInfo.php?id=153452345353"; // this is the request
with id in GET

Response

<response>
<data id="153452345353">..</data>
</response>

Would be much easier for me however if something already exists within
XHR and I dont need to make any changes to schema of the generated DSV
string (Ive just used XML in the above example for simplicity)


Anyway this may not even be the cause of the bug as its one of those
very inconsistent bugs that when you perform the same steps it works
the next 100 times however doesn't give the client much confidence. I
think it would be a good idea to ensure that only a single request is
being made/handled anyway so these steps are perhaps a good idea. If
anyone has any techniques or have encountered similar problems please
let me know, would be very much appreciated as this is the first time
Ive really had to consider this issue.

Thanks
Burnsy
 
T

Thomas 'PointedEars' Lahn

bizt said:
[...]
function doRequest() {
var oAjax = new AjaxWrapper;

Since constructing an object using a constructor is essentially a
constructor *call* (even though the algorithm is [[Construct]]),
I recommend providing an explicit argument list to indicate that:

var oAjax = new AjaxWrapper();
oAjax.url = "data/getInfo.php";
oAjax.onSuccess = function() {
// handler code goes here
}
oAjax.get(); // this command makes the request
}

Problem:
[...]
I'm trying to identify a bug in which I suspect two requests that
share global variables (outside the caller function - doRequest in the
above example) are very occasionally being executed at the same time.
Now, I have to accept that this can happen as I have a feature that
automatically makes the requests without the users command but also
the user can make the same request at any time by other means. Anyway
I think that perhaps whilst one request is being made and another in
parallel at the same time when they are both being handled because
they share some variables one requests handling may be screwing up the
others.

Solution(?):

Don't use global variables with concurrent requests.
I decided that it should be a good idea to have some kinda of flag
that tells the system that this process is currently being handled, go
away. That way I wont have two requests being handled at the same
time. In fact, better yet may even be to have a flag to say that a
request is under way to prevent another call to the server even being
made. Good idea?

No. Requests should be queued then, the second request being made in the
event listener for handling the response to the first request.
Incidentally, you need only one XHR object then. Modifying your wrapper
object so that it queues requests when necessary should prove simple.
Now what I really could also do with, when I make a request it would
be neat if I could log the request ID if exists and when I get the
response I can check its request ID to see if it was the last request
made. Does XHR have this included

No. RTFM.
or do I need to pass this when I make my request and return it as
part of my response generated.

Yes.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top