Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Jim Proctor

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!

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

"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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...