Why does this JavaScript work on Safari, but not F

2019-04-10 16:23发布

问题:

I have 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???

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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);