Jump to content
Larry Ullman's Book Forums

Chapter 11 Can'T Get Ajax To Work On Ie Or Chrome


Recommended Posts

Hello all.  I the 'test.js' examples to run smoothly on Firefox, however it doesn't work on IE9 or chrome.  I tried it with both my code and the downloaded code.  The code produces no error but the data is not retrieved.  Problem seemd to be at 'ajax.onreadystatechange = function() {...'.  I placed an alert both before and after and the one before is the only one that pops up.  Any ideas what I am doing wrong or what the problem is?  Thank you in advance!

Link to comment
Share on other sites

There's most likely a syntax error that Firefox is able to ignore/assume the meaning of, but Chrome and IE9 are not.

From the Chrome settings menu in the top-right corner, go to "Tools" and then "JavaScript console", and see if any errors are reported to the console when you run the Ajax request.

Please let us know what you find from that.

 

Thanks.

  • Upvote 1
Link to comment
Share on other sites

2 error messages: XMLHttpRequest cannot open file file///C:/ users/.../resources/test.txt. Cross origin requests are only supported  for    HTTP.    And the second is 'Uncaught error: NETWORK_ERR: XMLHttpRequest Exception 101.  I also had an issue with the status code and after researching online found I had to add another statement allow status = 0 to pass.  That was how I got the code to work on firefox.

Link to comment
Share on other sites

If you're running your script locally, try running it through a virtual server, and then place the text file somewhere within the virtual server web directory.

 

It sounds like a CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) issue, which can likely only be resolved by running your script through a server and putting the text file you're trying to access in a directory that is local to the server.

 

That make sense?

  • Upvote 1
Link to comment
Share on other sites

Put the script and the test.txt file in a new folder (the same folder) under htdocs, change the path from the script to the text file to simply 'test.txt', make sure your local Apache server is running, and then make sure you're actually going through your localhost to run the script (in other words, the URL should start with "http://localhost/", or just "localhost/").

  • Upvote 2
Link to comment
Share on other sites

Well as they say we learn more from our mistakes than out successes.  That solves the issue, I was launching the different browsers from inside of notebook++ instead of running themthrough localhost/testhtml.  Solved all issues thank you very much for your help.

Link to comment
Share on other sites

  • 2 weeks later...

Yes I tried.

This is the error I get

 

 

 

XMLHttpRequest cannot load file://localhost/Applications/MAMP/htdocs/chapter10examples/test.txt. Cross origin requests are only supported for HTTP. test.html:1
Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 test.js:15
 
Im running it from 
file://localhost/Applications/MAMP/htdocs/chapter10examples/test.html
Link to comment
Share on other sites

 Share

×
×
  • Create New...