Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-04-2007, 10:39 AM   PM User | #1
Crash1hd
Regular Coder

 
Join Date: Jul 2002
Location: 51° 03' -78" N -114° 05' 72" W
Posts: 617
Thanks: 0
Thanked 0 Times in 0 Posts
Crash1hd is an unknown quantity at this point
Ajax and Firefox

Hello,

I cant figure out why the code below is not working in Firefox it works fine in IE?

Code:
function sndReq(action) {
     http.open('get', 'page.php?action='+action);
     http.onreadystatechange = function() {
	switch(http.readyState){
		case 1:
		alert(1);
		break;
		case 2:
		alert(2);
		break;
		case 3:
		alert(3);
		break;
		case 4:
		alert(4);
		break;
	};
     }
     http.send(null);
}
In IE I get alerts for all 4 states but in FireFox I only get alert 4 what gives..

added code for knowledge

Code:
var http_request = false;
var http = false;
function getHTTPObj() {
	try {
		http_request = new XMLHttpRequest();
	}
	catch (e) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				http_request = false;
				alert("Your Browser does not work with Web 2.0!\nPlease Update");
			}
		}
	}
	return http_request;
}

var http = getHTTPObj();
Crash1hd is offline   Reply With Quote
Old 04-04-2007, 04:43 PM   PM User | #2
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
I had never done much with the states 1-3 before since I read they mean slightly different things in different browsers. But doing some tests and looking at your code I think the problem is that for some reason the default ajax request on firefox is synchronous (I thought it was async).

So you need to add a third parameter to your open command with true as the value.

http.open('get', 'page.php?action='+action, true);

That makes the onreadystatechange fire for 1-3 in FF as well.

david_kw
david_kw is offline   Reply With Quote
Old 04-04-2007, 09:25 PM   PM User | #3
snake_eyes
New Coder

 
Join Date: Mar 2007
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
snake_eyes is an unknown quantity at this point
hello,

dear, much better to pass the variable in send like:

sndreq.send(action=action);

try to use the following code, it is tested on safari, FF and IE:

Code:
var http_request = false;
	
    if (window.XMLHttpRequest)
	{
		// Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
        }
    }
	else if (window.ActiveXObject)
	{
		// IE
        try
		{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
		catch (e)
		{
        	try
			{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
			catch (e){}
        }
    }
    if (!http_request)
	{
        alert('Giving up :( Cannot create an XMLHTTP instance');

		return false;
    }
regards
snake_eyes is offline   Reply With Quote
Old 04-04-2007, 09:31 PM   PM User | #4
Crash1hd
Regular Coder

 
Join Date: Jul 2002
Location: 51° 03' -78" N -114° 05' 72" W
Posts: 617
Thanks: 0
Thanked 0 Times in 0 Posts
Crash1hd is an unknown quantity at this point
Hello,

Thankyou for the reply.

That worked great. But I am curious where would I look into checking to see about the ajax being syncronous and not asyncronous? Is this something fixable? I would rather it be asyncronous.
Crash1hd is offline   Reply With Quote
Old 04-05-2007, 01:04 AM   PM User | #5
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
That 3rd parameter to the open() function determines it. If true it is async, false is sync. If you pass only two parameters it is supposed to be async (according to my documentation) and appears to be in IE, but in FF the default is sync apparently.

Long and short. Always pass true for the 3rd parameter and you should be good to go with asynchronous calls.

david_kw
david_kw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:25 PM.


Advertisement
Log in to turn off these ads.