views:

106

answers:

5

I have the below HTML file. I tried the code on Safari and it was working fine. But when I tried this on Firefox, it’s not working.Can anyone suggest how to make it working on firefox?

On click on undo button I want to retrieve contents from the jsp file. Thats working when I used this code on safari on my mac.. but when I open the same file using firefox its not working. I am not sure is it due to browser settings or due to some other reason. I checked browser setting of firefox 3.6.12 installed on mac also it is enabled for javascript and java...

When I checked on HTTPfox it showed in Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) in the contents

Can anyone suggest whats going wrong???

<html><head></head>

<body>
<button type="button" onClick="handleButtonClick();">undo</button>
<button type="button">redo</button>
<select><option value="V1">V1</option>
<option value="V2">V2</option>
<option value="V3">V3</option>
<option value="V4">V4</option>
<option value="V5">V5</option></select>  

<script type="text/javascript">
function handleButtonClick(){var xmlHttp, handleRequestStateChange; 
handleRequestStateChange = function() {if (xmlHttp.readyState==4 && xmlHttp.status==200) { var substring=xmlHttp.responseText; alert(substring); } }      

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://csce.unl.edu:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true);
xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null);}
</script>

</body>
</html>
A: 

Use JQuery. It has an AJAX library which does these browser compatibility checks for you.

Also, Firebug may come in handy, to see whether the request is being sent and see what the response is.

Christian Mann
Why was this downvoted?
Christian Mann
Probably for being one of those people who throws jQuery at every problem.
Rob
Mmm. I see. That makes sense. My tip about Firebug still stands.
Christian Mann
+5  A: 

XMLHttpRequests only work when the request is on the same domain as the JavaScript making the request. So, your call to xmlHttp.open() would only work if that HTML file was hosted on csce.unl.edu.

Paul D. Waite
Thanks for the response. But then the same file works when I try to access from safari. I am not sure if its more of browser problem...
Judy
@Judy: Sure, but where is the file located? What do you type into Safari and Firefox’s address bar to look at it?
Paul D. Waite
it is on my local machine...for both cases.... I installed firefox on mac and tried to open the same file its not working for firefox... so the problem is I think browser specific but I am not sure whats going wrong :(
Judy
@Judy: ah, okay. Could you try popping in this doctype at the very start of the HTML file? `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">` I wouldn’t have thought that would change things, but it’s worth a go.
Paul D. Waite
It sounds like Safari is letting you violate the JavaScript domain restrictions when you're working locally. If you put this on a public website (one that Safari doesn't trust), I'm guessing it will fail for both browsers.
xscott
@Judy: I’ve just copied your code into a file on my machine, and served that file from localhost and localhost:8000 (altering the URL that the `XMLHttpRequest` requests to match). Works fine for me in Safari 5, Firefox 3.6 and Firefox 3 on Mac OS X 10.6. What browser versions are you using?
Paul D. Waite
Thanks for your efforts..I downloaded the latest version 3.6.12 for mac...I want the alert message to be displayed... which is displayed in safari but not in firefox either on ubuntu or on mac...
Judy
I checked then security settings of the firefox browser... The javascript and java is enabled there... I am not sure whats going wrong..
Judy
@Judy: What *is* displayed in Firefox? Does the `alert` dialog box appear? If so, what’s in it?
Paul D. Waite
I would look into JSONP as a solution to this.
Slappy
+2  A: 

Can the ubuntu box access the url http://csce.unl.edu:8080 ? It may be network/proxy/firewall settings on the virtual machine or in Firefox settings.

AUSteve
+2  A: 

I'd try running firefox on the Mac and see where that takes me. If that doesn't work Then the problem is the browser, if it does, it's the way you are loading the site

Nico
Yes I tried it doesn't work...its a browser problem..
Judy
A: 

I opened firebug > console and pasted

var xmlHttp, handleRequestStateChange; 
handleRequestStateChange = function() {if (xmlHttp.readyState==4 && xmlHttp.status==200) { var substring=xmlHttp.responseText; alert(substring); } }      

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://csce.unl.edu:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true);
xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null);

And i saw everything working. What the error exactly? Can you open firebug and look at javascript errors.

Edit try this:

 var req = new XMLHttpRequest();  
 req.open('GET', '/');  
 req.onreadystatechange = function (aEvt) {  
   if (req.readyState == 4) {  
      if(req.status == 200)  
       alert(req.responseText);  
      else  
       alert("Error loading page\n");  
   }  
 };  
req.send(null);
Amir Raminfar
Hi, Thanks for your efforts. The problem is that I am not getting the results from the jsp message as alert which I am getting in safari.
Judy
I still do recommend using jquery instead of doing that because ajax is different for each browser.
Amir Raminfar