473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XMLHttpRequest Synchronous mode not work in Firefox

Hello
I'm testing the XMLHttpRequest object in Firefox and IE. The code
below works well in IE and Firefox. It shows "1" when the string is a
number and "0" when not. The page aspxTest.aspx only write "0" or "1"
with a "response.write" method.
The problem that I have is when I try this example with Synchronous
mode. If I change the function sendNum with:

xmlhttp.open("GET",url,false);

In IE works well (show "0" or "1") but in Firefox don't show anything.
I have put an alert in "end" function before the condition of
xmlhttp.readyState but anything is writed. What's is wrong?

Thanks a lot
<html>
<head>
<script>
var xmlhttp;
function sendNum ()
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
}
catch (E)
{
xmlhttp=false
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp=false
}
}

xmlhttp = new XMLHttpRequest();
var url = "http://localhost/Test/aspxTest.aspx?num=" +
document.forms['f'].elements['num'].value;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange = end;
xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')
xmlhttp.send(null);
}

function end ()
{
//alert("end function");
if (xmlhttp.readyState == 4)
{
alert(xmlhttp.responseText);
}
}
</script>
</head>
<body>
<form id="f">
Number: <input type="text" width="100px" id="num">
<input type="button" value="is a number" onclick="sendNum()">
</form>
</body>
</html>
Jul 23 '05 #1
9 17964


David wrote:

I'm testing the XMLHttpRequest object in Firefox and IE. The code
below works well in IE and Firefox. It shows "1" when the string is a
number and "0" when not. The page aspxTest.aspx only write "0" or "1"
with a "response.write" method.
The problem that I have is when I try this example with Synchronous
mode. If I change the function sendNum with:

xmlhttp.open("GET",url,false);


You shouldn't use the onreadystatechange event handler if you are using
a synchronous request, instead then you should do e.g.
xmlhttp = new XMLHttpRequest();
var url = "http://localhost/Test/aspxTest.aspx?num=" +
document.forms['f'].elements['num'].value;
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')
xmlhttp.send(null);
if (xmlhttp.status == 200) {
// use xmlhttp.responseText or responseXML here
}
else {
// handle different response status here
}

that is simply place your code consuming the response after send(null),
as that method blocks.

But usually on the web you shouldn't use synchronous mode at all as it
blocks the browser and the user interface. For testing it might be fine
to start with synchronous requests but as you already know about
asynchronous requests and onreadystatechange simply use that.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote:
But usually on the web you shouldn't use synchronous mode at all as it
blocks the browser and the user interface. For testing it might be fine
to start with synchronous requests but as you already know about
asynchronous requests and onreadystatechange simply use that.


I did not know that XMLHttpRequest had a synchronous mode. Very interesting.

Indeed I think, synchronous mode has some advantages because JS's event
driven approach does not lend itself well to some problems. Synchronous
is fine, as long as you can impose a timeout. Can you do this with
XMLHttpRequest?

Cheers
Daniel Kabs
Germany

Jul 23 '05 #3


Daniel Kabs wrote:
Indeed I think, synchronous mode has some advantages because JS's event
driven approach does not lend itself well to some problems. Synchronous
is fine, as long as you can impose a timeout. Can you do this with
XMLHttpRequest?


As script in current browsers like Mozilla runs in the user interface
thread a synchronous request means that the user interface is blocked
while the request is being performed and while the response is being
received.
XMLHttpRequest in Mozilla and in Opera does not have a method to set
timeouts, neither has Msxml2.XMLHTTP used client side in IE.
The Msxml2.ServerXMLHTTP object to be used on the server (e.g. in ASP
pages) has a method to set timeouts:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmobjXMLDOMServerXMLHTTP.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmthsetTimeoutsMethod.asp>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Hello Martin,

this thread is close to expire on my news server. How can you answer a
posting to a more than two months old thread in just a couple of
minutes? :-)

Martin Honnen wrote:
As script in current browsers like Mozilla runs in the user interface
thread a synchronous request means that the user interface is blocked
while the request is being performed and while the response is being
received.
XMLHttpRequest in Mozilla and in Opera does not have a method to set
timeouts, neither has Msxml2.XMLHTTP used client side in IE.


Thanks for pointing that out. So one has to *rely* on the fact, that the
server's response is swift. Not nice, but the alternative is an
asynchronous request, which is also not without ramifications.

Cheers
Daniel Kabs
Germany
Jul 23 '05 #5
On Fri, 29 Apr 2005 16:02:31 +0200, Daniel Kabs <da*********@gmx.de>
wrote:
Hello Martin,

this thread is close to expire on my news server. How can you answer a
posting to a more than two months old thread in just a couple of
minutes? :-)

Martin Honnen wrote:
As script in current browsers like Mozilla runs in the user interface
thread a synchronous request means that the user interface is blocked
while the request is being performed and while the response is being
received.
XMLHttpRequest in Mozilla and in Opera does not have a method to set
timeouts, neither has Msxml2.XMLHTTP used client side in IE.


Thanks for pointing that out. So one has to *rely* on the fact, that the
server's response is swift. Not nice, but the alternative is an
asynchronous request, which is also not without ramifications.


No, always use async, sync is very bad on anything with a UI.

Jim.
Jul 23 '05 #6
Hello Jim,

I came across your pages while searching for help on some javascript
problems and I just wanted to say that I like your jibbering.com website.

Jim Ley wrote:
Thanks for pointing that out. So one has to *rely* on the fact, that the
server's response is swift. Not nice, but the alternative is an
asynchronous request, which is also not without ramifications.


No, always use async, sync is very bad on anything with a UI.


I've read that on your page, too. But asynchronous programming can be
awkward. Imagine you have several object, one object calls a method of
another object and so on. The last method invoked in this "cascade" is
supposed to load an image and return the image object. Now this can not
be accomplished in JS because the image is going to be loaded in an
async way and the method returns immediately. All the functions have to
return, the stack unwinds and the status is lost, if it is not saved
in e.g. callbacks using closures. And every method in this "cascade" has
to support callbacks, just because the last function is async. See the
sequence diagram in [2].

A solution could be to wrap the async functions with a "Synchronous
Wrapper Facade in Javascript" (if that is possible in JS, but I doubt
it). Please have a look at my corresponding question in [2].

Cheers
Daniel Kabs
Germany
--
References:
1 <42***********************@newsread4.arcor-online.net>
2 <42***********************@newsread4.arcor-online.net>
Jul 23 '05 #7
On Mon, 02 May 2005 12:44:36 +0200, Daniel Kabs <da*********@gmx.de>
wrote:
No, always use async, sync is very bad on anything with a UI.


I've read that on your page, too. But asynchronous programming can be
awkward.


No more awkward than all the rest of javascript/html which relies on
it, it's all event based, I can't see how if you can cope with that
you'll have any more of a problem with this?

It's not awkward, it's just a different way of programming, and as you
have to use it for the rest, I fail to see the problem. The "load an
object, which loads an object, which loads an object which returns an
image" is simply not a pattern you should be using in webpages, I
don't think it's useful to try and create hacks and workarounds to fit
that pattern, just pick a more appropriate one.

Sync simply violates any user interface rules that the interface
cannot lock up. The big problem is that people test locally, or with
fast, reliable connections to their server, whereas the users will be
a long way away on unreliable connections, and they'll get very
different behavour.

Then there's the number of complete lock-up bugs when there are
multiple sync requests going on, it's simply not appropriate.

Jim
Jul 23 '05 #8
Hello!

Jim Ley wrote:
It's not awkward, it's just a different way of programming, and as you
have to use it for the rest, I fail to see the problem. The "load an
object, which loads an object, which loads an object which returns an
image" is simply not a pattern you should be using in webpages, I
don't think it's useful to try and create hacks and workarounds to fit
that pattern, just pick a more appropriate one.


I'm sorry if you received the impression that I would implement "load"
requests by recursive object calls. That's not what I meant when I wrote
"one object calls another". And of course I do not want the UA/UI to
freeze. Maybe I was too unprecise.

The problem is not asynchronous programming per se. I've done that
before. The problem is that Javascript provides no means of waiting for
results, i.e. it's a completely async environment.

Thus (as I wrote before) if you have a method calling another method
calling another method doing something async, even the first
method/function has to provide for this async behaviour and pass a
closure as parameter to continue after the async call has finished and
triggers the event handler. I find this "awkward" because this prevents
to "abstract away" deeper levels of functionality as even the highest
abstraction level has to know about the async type of the underlying
levels. Or am I completely mistaken?

Cheers
Daniel



Jul 23 '05 #9
Hello Richard,

thanks for sharing your ideas and the detailed description how a
completely event-driven system works.

Richard Cornford wrote:
Awkwardness is going to be largely a matter of perception. Procedural
code doesn't lend itself to asynchronous operation but OO code shouldn't
be such a problem. In OO the 'state' should be a quality of the objects
not the stack.


Yes, it's the feeling that makes one prefer a certain design. I would
not rule out procedural code for async operations. E.g. I imaging that
implementing a network protocol using a statemachine is an appropriate
approach. In OO code you would break up the case/switch statements and
"spread them around", delegate it to objects and their methods. Now the
objects keep their own state (and implement a statemachine). Is this
advantageous, I don't know.

Maybe it's just because I have a background in procedural programming.
So designing a completely event-driven system, where objects interact
sending async messages seems counter-intuitive because it lacks a main
flow of control.

I found the following example helpful:
In procedural programming you often have the control flow in a main
routine that calls other components only to do lower-level tasks. The
main routine can be long and complex but it gives the whole thing a
logical structure.

In an event-driven program you don't have that control flow in the main
routine. The main routine sends of messages and mainly consists of event
handlers. Not the main routine but the objects are now "in charge" and
not only sequentially, but also concurrently as messages and events may
happen any time. So the logical structure of the program is scattered
around and interactions among objects may be hard to understand.
Additionally, the use of ordinary linguistic constructs like loops,
stack allocated variables, ... is limited as these disappear accross
callbacks/messages.

Well, maybe one just has to get used to it :-) I'd like to see a real
good example of an event-driven system with object interaction through
messages implemented using Javascript.
See the sequence diagram in [2].


I see no sequence diagram.


Because there is none :-) I referred to the wrong footnote. See here for
the sequence diagramm instead:
<42***********************@newsread4.arcor-online.net>
http://groups.google.de/group/de.com...dba787abae68d4
Cheers
Daniel
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
8
by: Spasmoid | last post by:
I have the following code running in FireFox 1.5 (a remote XUL application): var req = new XMLHttpRequest(); // execution will block until request is completed // third param set to false...
2
by: petermichaux | last post by:
Hi, I thought it is about time I tried writing some JavaScript with XMLHttpRequest instead of just using the Yahoo! UI library. The simple page below works in both Safari and Opera but I don't...
13
by: TLaufenberg | last post by:
I'm new to Javascript programming and I've run into a bit of a snag with making an XMLHttpRequest in the Safari browser. Actually, the request doesn't work in Firefox either but only when I use a...
3
by: perrog | last post by:
Hi! What is the expected behaviour when you send an XmlHttpRequest just before the page is about to unload? I'm sending a XmlHttpRequest on an onClick event, and I can inspect that the request...
10
by: HugeBob | last post by:
Hi All, I'm having a problem with a web app I'm writing. I have a form for which I'm going to validate one of the fields. I use AJAX to retrieve XML containing a list of valid entries. My app...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
4
by: jackchang1 | last post by:
Hi, group I have a problem with IE when using XMLHttpRequest object in asynchronous mode. I have a page, and when it is closed, the onunload event handler of the page will create an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.