473,385 Members | 1,521 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,385 software developers and data experts.

Ajax problem

107 100+
hi guys,

My code suddenly stopped working

here it is:


[HTML] function Ajax(page,data){

page = "includes/" + page;

var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

var output;

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
output = xmlHttp.responseText;
}
}

xmlHttp.open("POST",page,false);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(data);


return output;

}[/HTML]


It works in firefox 2 (with firebug installed and enabled) but onreadystatechange doesnt seem to get called in IE (7 and before) and firefox 3.

Can anyone see why?? It has been working fine and I dont think ive changed this function but after an afternoon of debugging ive narrowed it down to this error. I also just tried replacing the "output = xmlHttp.responseText;" line to use a callback function like most online examples do, but still no joy.


Cheers

Andy
Jun 16 '08 #1
7 1140
acoder
16,027 Expert Mod 8TB
You're making a synchronous request as opposed to an asynchronous one. Are you sure that's what you want?
Jun 16 '08 #2
theS70RM
107 100+
is that because im making the ajax function return the output from the request rather than using a callback function?

I think its ok that way for what im doing as its processing a form and the page is frozen till the action is complete anyway...

Not my main problem though, and thats that onreadystate doesnt get called anymore.

cheers for the response

Andy
Jun 17 '08 #3
acoder
16,027 Expert Mod 8TB
If it's synchronous, the onreadystatechange will never fire, so output won't be set. Return the responseText directly after send() in place of 'output'.
Jun 17 '08 #4
theS70RM
107 100+
ok, im a little lost then.

Thanks, that works by the way!

How would you make this function asynchronous?
Jun 17 '08 #5
acoder
16,027 Expert Mod 8TB
Since it's synchronous, it never needs to let you know when it's complete via onreadystatechange. You'll know when the browser stops hanging. It stops at send() and waits until the request is complete, so you can safely return responseText after it, a bit like how a prompt/alert is modal.

To make it asynchronous, use "true" for the last parameter of the open() method.
Jun 17 '08 #6
theS70RM
107 100+
ah ok, i think i understand now.

Thanks for your help!
Jun 17 '08 #7
acoder
16,027 Expert Mod 8TB
No problem at all, you're welcome :)
Jun 17 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
3
by: Alok yadav | last post by:
I have an open IP and on that IP our main application is hosted. it uses ajax. in web.config file i have register ajax handlers. there are also other sites or project on that IP. now my problem is...
3
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
2
by: majidtou | last post by:
Hi , We have an HTML page which is using AJAX. We make an AJAX call which retrieves a table with mutiple rows. We need to extend this functionality. In the table, we want to put a button on each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.