Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why my code is not working ??

when i trying to run the code in the browser it says there is an error in the log XMLHttpRequest part index.html:17 XMLHttpRequest cannot load another-idex.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

And this is the code :-

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if(xhr.readyState === 4) { document.getElementById('ajax').innerHTML = xhr.responseText; } }; xhr.open('GET', 'another-idex.html'); xhr.send();

5 Answers

It may be that you are using your local file system to request the file, chrome doesn't allow file:// requests. Either try hosting it on workspaces or try a local server solution like AMPPS or MAMP.

Simon Coates
Simon Coates
28,694 Points

i ran it through workspaces with the exact same code and it worked fine. I'm inclined to agree with tate. The examples i saw online with this error seemed to be accidental use of the file:// protocol.

Thank Tate & Simon for replying i did, and still have the same problem should i read about PHP first ??

Simon Coates
Simon Coates
28,694 Points

when you access the file that contains the javascript, what protocol are you using? does the url start with http?

No, It''s not start with http how i should make it start with http ??

Simon Coates
Simon Coates
28,694 Points

You need to be accessing it through a server (in which case it should use http or https). This doesn't require understanding php, as the files you are using aren't dynamic. You just need to copy your files into a place where your browser can request them with http. The obvious ways to do this are to use a treehouse workspace, or create a localhost server. tate recommended AMPPS or MAMP, but if you use windows Wamp, or xampp. (again, while these servers have PHP, given what you're trying to do, you don't need to use it). The simplest option is open a workspace and copy your files in. Click the preview workspace icon. (this bypasses the need to install a local server - which can sometimes have issues.).

(this advice is on the assumption you're trying to access the files on your local machine without using a server - which would use the file:// protocol)

Ok Simon, Thank you :)