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

Problem with XMLhttp

Hi,

I am having a bit of a problem with using xmlhttp. The code of the javascript file is shown below used in Windows XP.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. var xmlhttp = null;
  4.  
  5. function SetURLDiv(url) {
  6.   if (window.XMLHttpRequest) {
  7.     // code for all new browsers
  8.     xmlhttp=new XMLHttpRequest();
  9.    }
  10.   else if (window.ActiveXObject) {
  11.     // code for IE5 and IE6
  12.    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.   }
  14.   if (xmlhttp!=null) {
  15.     xmlhttp.onreadystatechange=setNewDiv;
  16.     xmlhttp.open("GET",url,true);
  17.     xmlhttp.send(null);
  18.   }
  19.   else {
  20.     alert("Your browser was unable to download the required help.");
  21.   }
  22. }
  23.  
  24. function setNewDiv() {
  25.   if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) {  // 4 = "loaded"  200 = ok
  26.     // firstly remove the old help.
  27.     var unwantedDiv = document.getElementById('Insert');
  28.     unwantedDiv.parentNode.removeChild(unwantedDiv);
  29.     // Now install the new help.
  30.     document.getElementById('InsertSite').innerHTML=xmlhttp.responseText;
  31.   }
  32. }
  33.  
  34. function printURL(url) {
  35.   if (window.XMLHttpRequest) {
  36.     // code for all new browsers
  37.     xmlhttp=new XMLHttpRequest();
  38.    }
  39.   else if (window.ActiveXObject) {
  40.     // code for IE5 and IE6
  41.    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  42.   }
  43.   if (xmlhttp!=null) {
  44.     alert('One');
  45.     xmlhttp.onreadystatechange=printPage;
  46.     xmlhttp.open("GET",url,true);
  47.     xmlhttp.send(null);
  48.   }
  49.   else {
  50.     alert("Your browser was unable to download the required information.");
  51.   }
  52. }
  53.  
  54. function printPage() {
  55.   alert('Two');
  56.   alert(xmlhttp.readyState + '  ' + xmlhttp.status);
  57.   alert(xmlhttp.responseText);
  58.   if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) {  // 4 = "loaded"
  59.     alert('Three');
  60.     var printDiv = document.getElementById('print');
  61.     document.getElementById('print').innerHTML=xmlhttp.responseText;
  62.     window.print();
  63.   }
  64. }
  65.  
  66.  
The function setURLDiv works fine in both Firefox and IE. Exactly as expected.

However the function printURL doesn't.

In Firefox gets through to the printPage function with the readyState increasing from 1 to 4 but the status always returns as 0. From readyState == 3 or 4 the responseText returns correctly in full. So the script never gets to alert('Three');

If I remove the status test then the whole think works fine and prints the page correctly.

In IE the script gets through to alert('One') but the function printPage is never called since alert('Two') is never displayed.

I originally simply copied, pasted (and modified the function name etc) so the different results are puzzling.

I would welcome any ideas on what is wrong because I am at a loss at the moment. Any ideas on what to test.?

Cheers

Andrew
Apr 9 '08 #1
3 2557
acoder
16,027 Expert Mod 8TB
Welcome to bytes.com!

I would suggest that you use different variables for the two requests in case the two are conflicting, e.g. xmlhttp2 for the printURL and printPage.

PS. thanks for using code tags. You forgot, though, to close the tag properly by using a space in the end tag.
Apr 9 '08 #2
Welcome to bytes.com!

I would suggest that you use different variables for the two requests in case the two are conflicting, e.g. xmlhttp2 for the printURL and printPage.

PS. thanks for using code tags. You forgot, though, to close the tag properly by using a space in the end tag.

Thanks for the reply.

I don't know why but after fooling around for another couple of hours I got someone else to try it out from our website over the internet and it worked. ?? So I went back and tried yet again from my own computer and it too worked. The code was more or less the same, and nothing likely to affect the problem. So I still don't know what went wrong.

I did add the closing tag originally but obviously it got lost in the paste and I didn't pick it up.

Cheers

Andrew
Apr 9 '08 #3
acoder
16,027 Expert Mod 8TB
No problem, glad you've got it working. If you have any problems, post back to the forum and we'll see what we can do.
Apr 9 '08 #4

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

Similar topics

9
by: fochie | last post by:
Greetings, I'm having a problem when I try to GET a file from my server via xmlhttp when using Mozilla. With IE I can get any type of file fine, get/display headers fine, etc. With Mozilla,...
12
by: Botan Guner | last post by:
Hi all, Here is the problem, i'm using Microsoft.XMLHTTP for ie and XMLHttpRequest for mozilla, on my local server which is win2000 server i've no problem with that but when i uploaded the file...
14
by: squash | last post by:
The following code works fine in Firefox/Netscape but wont work in IE. I suspect the problem is with one of these two simple functions. If there is no obvious error Ill paste the entire code. ...
1
by: danep | last post by:
Hi, I'm fairly new to AJAX, but I've been able to retrieve HTML and plain-text documents without any trouble. However, I haven't figured out how to retrieve it in XML format. Basically, here's...
0
by: wasif | last post by:
I am trying to upload file using ajax and php but having some problems. it always says that there was a problem and file is not uploaded. here is the code form and ajax code <!DOCTYPE html...
2
by: K. | last post by:
Hello! I have the following problem. I have form div which is replaced by ajax event. Unofrtunately all the ajax inputs are null after posting the form (method="post") in Firefox, but on...
47
by: mukeshrasm | last post by:
Hi I am calling two pages using Ajax Get_Pages.php and Get_Content.php from combo box. Both pages are displayed based on selection from combo box. Main problem is that it is not showing the...
1
by: Christoph Boget | last post by:
I'm experiencing a very odd problem and it's happening only in IE6. IE7, Safari, Opera and Firefox are all working properly. What's happening is that I'm using XHR request responses to update the...
1
by: agun | last post by:
Hello, I'm having problem implementing AJAX on IE7. In other browser it works totally fine. In IE7, if i enable the Enable Native XMLHTTP support, everything just went wrong. Here is the...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.