Jim Proctor Posted March 17, 2013 Share Posted March 17, 2013 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 More sharing options...
HartleySan Posted March 17, 2013 Share Posted March 17, 2013 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. 1 Link to comment Share on other sites More sharing options...
Jim Proctor Posted March 18, 2013 Author Share Posted March 18, 2013 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 More sharing options...
HartleySan Posted March 18, 2013 Share Posted March 18, 2013 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? 1 Link to comment Share on other sites More sharing options...
Jim Proctor Posted March 18, 2013 Author Share Posted March 18, 2013 I have place it in xampp/htdocs and get the same results. getting frustrated Link to comment Share on other sites More sharing options...
HartleySan Posted March 18, 2013 Share Posted March 18, 2013 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/"). 2 Link to comment Share on other sites More sharing options...
Jim Proctor Posted March 18, 2013 Author Share Posted March 18, 2013 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 More sharing options...
HartleySan Posted March 19, 2013 Share Posted March 19, 2013 No problem. Glad it worked. I had a feeling that something like that was the issue. Link to comment Share on other sites More sharing options...
bawongcoding Posted March 28, 2013 Share Posted March 28, 2013 Hi, I am getting the same problems with MAMP on mac. I cant seem to get it loaded. Does anyone know how to get the first example of test.txt working by circumventing the CORS? or some other easy way to get up and running in MAMP? Thanks Link to comment Share on other sites More sharing options...
HartleySan Posted March 28, 2013 Share Posted March 28, 2013 I don't use Mac OS, and I've never used MAMP before, but did you try running the script through your localhost like I recommended above? Link to comment Share on other sites More sharing options...
bawongcoding Posted March 28, 2013 Share Posted March 28, 2013 Yes I tried. This is the error I get Failed to load resource file://localhost/Applications/MAMP/htdocs/chapter10examples/css/clean.css 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 More sharing options...
HartleySan Posted March 28, 2013 Share Posted March 28, 2013 "file" is the wrong protocol. If you're going through localhost, you need to use "http" or "https". In other words, your URLs should start with the following: http://localhost If you want to post the relevant code, then I can try to help more. Otherwise, there's not much else I can offer. Link to comment Share on other sites More sharing options...
Recommended Posts