views:

42

answers:

1

I have an aspx page on which I am using XDomainRequest object to populate two div(s) with html returned from AJAX response.
I have used Jquery to get the divs and perform "each()" on the retrieved List

 var divs = $("div");
 divs.each(function (index) 
 {
   if (window.XDomainRequest) {
            xdr = new XDomainRequest();
            if (xdr) {

                xdr.onload = function () {

                    alert("XDR Response - " + xdr.responseText);
           var currentDivID = divs[index].attributes["id"].value;
           var selectedDiv = $("div[id='" + currentDivID + "']");


                    if (xdr.responseText == '')
                        selectedDiv.attr("style", "display:none;");
                    else
                        selectedDiv.append(xdr.responseText);


                };

                xdr.open("GET", xdrUrl);

                try {

   xdr.send();

  } catch (e) {

                    alert(e);

                }


            } else {

                alert('Create XDR failed.');

            }

        } else {

            alert('XDR not found on window object .');

        }

 }

Now, whats happening is , i have two Divs on a page that have different IDs and when this code runs on "$.ready(function(){})" , both requests are asynchronously sent to the server and processed

the result is
1. sometimes the onload get the response for the second div in both div results.
2. IE sents only one request to the server(I am using fiddler to see what requests are sent to server).

Can anybody guide me whats wrong with the code ? As far as I know XDR does not support synchronous calls, and asynchronous calls are giving me wrong results. Any workaround/tip for this problem.

A: 

Issue solved by myself when I pointed out a mistake in my code:(.

xdr = new XDomainRequest(); 

should be

var xdr = new XDomainRequest(); 

For Point 2 , I added "Cache-Control:no-cache" header in my response and it solved the matter.

Furqan