How to force XMLHttpRequest to be synchronous - ("GET",addrServletURL,false) is not synchronous 100%
enter code here
Below is my code
When connecting with IE browser the client will wait the server response in order to handle the data parsing - the server takes some time to send back the result to the client because it’s calling an external system in order to get the data
So to resume I have no problem when IE is my browser
But indeed when my browser is Safari in the below code (please see my full code )the alert will be shown after a while which means the client is not wa开发者_StackOverflow中文版iting the server response and this creates a potential problem for me . My question how I can force the client to wait the server response when using XMLHttpRequest with Safri
xmlDoc= new XMLHttpRequest();
xmlDoc.open("GET",addrServletURL,false);
xmlDoc.send("");
// Check results
if ((xmlDoc == null) ||(xmlDoc.responseXML == null)){
alert(pageDefs.msg["ADDRESS_HELP_NOT_AVAILABLE"]);
return result;
}
nodes=xmlDoc.responseXML.documentElement.childNodes;
topElement=xmlDoc.responseXML.documentElement.nodeName;
From my experience, the synchronous request approach tends not to work stable across browsers.
If you want to parse the response once it has fully loaded, use an asynchronous request, bind a function to the onreadystatechange
property and check readyState == 4
(i.e. fully loaded) then process it.
Why do you want to do synchronous transfers? You could use a callback and display a message to the users the request is not yet done.
xmlDoc.onstatechange = function(){
if ( xmlDoc.readyState == 4 && xmlDoc.responseXML ){
// Execute important other code here. responseXML is loaded.
// Check results
if ((xmlDoc == null) ||(xmlDoc.responseXML == null)){
alert(pageDefs.msg["ADDRESS_HELP_NOT_AVAILABLE"]);
return callback(result);
}
nodes=xmlDoc.responseXML.documentElement.childNodes;
topElement=xmlDoc.responseXML.documentElement.nodeName;
}
};
wrap this in
doStuff(callback){
// xmldoc code here.
}
and you should be fine.
精彩评论