By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Problem with XMLHttpRequest in java script

P: 83
Hi,

I'm new on this forum so any help would be much appreciated. Have just spent 3 hours searching various forums but to no avail. Basically this works fine in IE but in Firefox (v2) it always gives the alert "Page unavailable. Please try again later." The call is on the same domain and i've tried switching post to get and syn to async etc but no luck...!

Expand|Select|Wrap|Line Numbers
  1.     if(typeof XMLHttpRequest != 'undefined'){ 
  2.         oXMLHTTP = new XMLHttpRequest(); 
  3.     } else if (window.ActiveXObject) {
  4.         oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
  5.     }
  6.  
  7.     var sURL = "xxxxx"
  8.     oXMLHTTP.onreadystatechange = managestatechange;
  9.     oXMLHTTP.open( "POST", sURL, false );
  10.  
  11.     try {
  12.         oXMLHTTP.send();
  13.         }
  14.         catch (e) {
  15.             alert("Page unavailable.  Please try again later.");
  16.         }
  17.  
  18.     //alert ("dupeText=" + dupeText);
  19.     return dupeText;    
  20.  
  21.  
  22. function managestatechange() {
  23.  
  24.     if (oXMLHTTP.readyState == 4) {
  25.         if (oXMLHTTP.status == 200) {
  26.             if (oXMLHTTP.responseText != "") {
  27.                 dupeText = oXMLHTTP.responseText 
  28.             }
  29.         } else {
  30.            alert("Network Unavailable - please try again later:\n" + oXMLHTTP.statusText);
  31.         }
  32.     }
  33.  
  34. }
Jan 29 '08 #1
Share this Question
Share on Google+
28 Replies


acoder
Expert Mod 15k+
P: 16,027
Hi Tim, welcome to TSDN!

Add the parameters to the send method. See an example here.
Jan 29 '08 #2

P: 83
Hi acoder,

thanks for your quick reply but still having probs which is no doubt down to me! I've modified my code (changed sURL and added paramString without the ?) but now nothing is returned. Maybe i misunderstood ?

I took

Expand|Select|Wrap|Line Numbers
  1. if(typeof XMLHttpRequest != 'undefined'){ 
  2.         oXMLHTTP = new XMLHttpRequest(); 
  3.     } else if (window.ActiveXObject) {
  4.         oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
  5.     }
  6.  
  7.     var sURL = "www.myDomain.com"
  8.     oXMLHTTP.onreadystatechange = managestatechange;
  9.     oXMLHTTP.open( "POST", sURL, false );
  10.  
  11.     try {
  12.         var dataString = "ID=2"
  13.         oXMLHTTP.send(paramString);
  14.         }
  15.         catch (e) {
  16.             alert("Page unavailable.  Please try again later.");
  17.         }
  18.  
  19.     //alert ("dupeText=" + dupeText);
  20.     return dupeText;    
  21.  
  22.  
  23. function managestatechange() {
  24.  
  25.     if (oXMLHTTP.readyState == 4) {
  26.         if (oXMLHTTP.status == 200) {
  27.             if (oXMLHTTP.responseText != "") {
  28.                 dupeText = oXMLHTTP.responseText 
  29.             }
  30.         } else {
  31.            alert("Network Unavailable - please try again later:\n" + oXMLHTTP.statusText);
  32.         }
  33.     }
  34.  
  35. }
Jan 29 '08 #3

acoder
Expert Mod 15k+
P: 16,027
The URL must be from the same domain. If you need to make a cross-domain request, you will need to do this from the server side.
Jan 29 '08 #4

P: 83
it is from the same domain...
Jan 29 '08 #5

acoder
Expert Mod 15k+
P: 16,027
paramString should be dataString.
Jan 29 '08 #6

P: 83
sorry my mistake, in the 'real code' it is datastring. just to reiterate the top example works fine with ie
Jan 29 '08 #7

acoder
Expert Mod 15k+
P: 16,027
dupeText will only be available after the readyState is 4, so the return dupeText in the first function will not be set.
Jan 29 '08 #8

P: 83
ah good point. i added dupeText = ""; as the first line of the function but still not working !
Jan 29 '08 #9

acoder
Expert Mod 15k+
P: 16,027
What is being set to the returned value, dupeText? You need to set this in the callback function.
Jan 29 '08 #10

P: 83
the function calls an asp page, runs a query and then does a response.write of the results at the end. the dupe text is assigned this final output...
Jan 29 '08 #11

acoder
Expert Mod 15k+
P: 16,027
Have you made sure this ASP page works fine in Firefox without Ajax?
Jan 29 '08 #12

P: 83
good thought but yes it works fine
Jan 29 '08 #13

acoder
Expert Mod 15k+
P: 16,027
You haven't set the request header. After calling the open method, add this line:
Expand|Select|Wrap|Line Numbers
  1. oXMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Jan 29 '08 #14

P: 83
still no joy. i've now simplified eveything to try and identify the prob. Now running on local webserver with this. again ie is fine and firefox doesn't work. In firefox the alert is never fired at the end


Expand|Select|Wrap|Line Numbers
  1.     var dupeText = "";
  2.     var xmlHttpReq=false;
  3.      var strURL = "http://localhost/cdvr/dev/testReturn.asp"
  4.  
  5.     if (!window.ActiveXObject) {
  6.         alert("Firefox");
  7.         xmlHttpReq = new XMLHttpRequest();  
  8.     } else {
  9.         alert("Microsoft"); 
  10.         xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
  11.     }
  12.  
  13.     xmlHttpReq.open('POST',strURL,false);
  14.     xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  15.  
  16.     xmlHttpReq.onreadystatechange = function() {
  17.         if (xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
  18.             dupeText = xmlHttpReq.responseText;
  19.         }
  20.     }
  21.  
  22.     xmlHttpReq.send();
  23.     alert ("dupeText=" + dupeText);
Jan 30 '08 #15

acoder
Expert Mod 15k+
P: 16,027
You need to make an asynchronous request. The third parameter of the open method should be true.
Jan 30 '08 #16

P: 83
hmmm...changed that. Firefox still dosen't work and now ie doesn't return the dupeText. can i pm you the files (one js, one asp)....
Jan 30 '08 #17

acoder
Expert Mod 15k+
P: 16,027
Put the alert within the anonymous function. If that doesn't work, attach the files here or if you don't want to put them here, you can PM them to me.
Jan 30 '08 #18

P: 83
still no joy but many thnks for offering to look at the files...

i have test.asp.......................................... ............................................

[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<script language="javascript" src="test.js"></script>
<input type="button" value="test me" onclick="testMe()">
</body>
</html>
[/HTML]
then testReturn.asp.................................... ..................................................

[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
hello
</body>
</html>
[/HTML]
and finally test.js........................................... ...........................................


Expand|Select|Wrap|Line Numbers
  1. function testMe() {
  2.  
  3.     var dupeText = "";
  4.     var xmlHttpReq=false;
  5.      var strURL = "http://localhost/cdvr/dev/testReturn.asp"
  6.  
  7.     if (!window.ActiveXObject) {
  8.         alert("Firefox");
  9.         xmlHttpReq = new XMLHttpRequest();  
  10.     } else {
  11.         alert("Microsoft"); 
  12.         xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
  13.     }
  14.  
  15.     xmlHttpReq.open('POST',strURL,true);
  16.     xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  17.  
  18.     xmlHttpReq.onreadystatechange = function() {
  19.         if (xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
  20.             dupeText = xmlHttpReq.responseText;
  21.             alert ("dupeText=" + dupeText);
  22.         }
  23.     }
  24.  
  25.     xmlHttpReq.send();
  26.  
  27. }
Jan 30 '08 #19

acoder
Expert Mod 15k+
P: 16,027
The code works fine. The only change I made was to make strURL relative.
Jan 30 '08 #20

P: 83
so you changed it to var strURL = "testReturn.asp",still doesn't work for me on firefox. are you on 2.0.0.11 ? i must be doing something really stupid here !
Jan 30 '08 #21

P: 83
at last !!!!!

i had to change

xmlHttpReq.send(data);

to

var data=""
xmlHttpReq.send(data);

as i finally figured out ff was throwing this error...

Error: uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/cdvr/dev/test.js :: testMe :: line 25" data: no]

Thanks for all your help, i really appreciate it, i'm going out to celebrate.......!
Jan 30 '08 #22

acoder
Expert Mod 15k+
P: 16,027
I see that you'd removed the string that you'd added previously to send().

Glad to hear that you finally got it working. Post back to the forum with any more questions.
Jan 30 '08 #23

P: 83
actually i do have another which is causing me more probs.

WHy in ff must i have

xmlHttpReq.open('POST',strURL,true);
and not

xmlHttpReq.open('POST',strURL,false);

true causes it run through the code without waiting which is not what i want
Jan 31 '08 #24

acoder
Expert Mod 15k+
P: 16,027
When it's true, it's asynchronous, which is what you should be using. Avoid a synchronous request if possible. When it's async. put the code that you want to execute when the response is ready in the onreadystatechange function.
Jan 31 '08 #25

P: 83
ok but have a js function call like this

Expand|Select|Wrap|Line Numbers
  1. var answer = checkDupeDate()
  2. alert ("answer=" + answer);
  3.  
  4. function checkDupeDate() {
  5.  
  6. .......
  7.  
  8. xmlHttpReq.open('POST',strURL,true);
  9. xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  10.  
  11. xmlHttpReq.onreadystatechange = function() {
  12. if (xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
  13.                 dupeText = xmlHttpReq.responseText;
  14.     alert (" firefox dupeText 1 = " + dupeText);
  15.     return dupeText;    
  16. }
  17.         }
  18.  
  19.            xmlHttpReq.send(params);
  20.  
  21. }
  22.  

Now when i call checkDupeDate it always returns nothing as it isn't waiting for the xmlHttpReq to do it's thing. i get

firstly alert(answer) which is blank
then alert (" firefox dupeText 1 = " + dupeText) which sites on top of the first alert
Jan 31 '08 #26

acoder
Expert Mod 15k+
P: 16,027
Put the code which requires answer/dupeText within the function after dupeText has been set to the responseText.

PS. please use [code] tags when posting code. Thanks!
Jan 31 '08 #27

P: 83
great, got their finally. may thanks again!
Jan 31 '08 #28

acoder
Expert Mod 15k+
P: 16,027
You're welcome again!
Jan 31 '08 #29

Post your reply

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