Identifying XHR requests / responses?? Preventing

Discussion in 'Javascript' started by bizt, Jul 10, 2009.

  1. bizt

    bizt Guest

    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
    bizt, Jul 10, 2009
    #1
    1. Advertising

  2. bizt wrote:
    > [...]
    > 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Jul 10, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2VsdmFrdW1hcg==?=

    Identifying the IE standard button requests in asp.net.

    =?Utf-8?B?U2VsdmFrdW1hcg==?=, Oct 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    413
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 25, 2005
  2. James Hawkins
    Replies:
    2
    Views:
    448
    James Hawkins
    Nov 16, 2006
  3. xkenneth

    View XMLRPC Requests/Responses?

    xkenneth, Oct 15, 2007, in forum: Python
    Replies:
    6
    Views:
    405
    Mark T
    Oct 16, 2007
  4. Lord0
    Replies:
    4
    Views:
    378
    Arne Vajhøj
    Mar 4, 2009
  5. Erich Lin

    multiple xhr requests

    Erich Lin, Jul 6, 2006, in forum: Ruby
    Replies:
    3
    Views:
    177
    Erich Lin
    Jul 7, 2006
Loading...

Share This Page