Jump to content

ajax


Matej

Recommended Posts

Hi , im trying to understand ajax , here is my script

<!DOCTYPE html><html><head><script>function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)  {  xmlhttp=new XMLHttpRequest();  }else  {  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("get","texttest.txt",true);xmlhttp.send();}</script></head><body> <div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" onclick="loadXMLDoc()">Change Content</button> </body></html> 

the texttest.txt contains just

<p>hello world</p>

but it isnt working , it writes Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file

 

can anyone help me please? :)

Edited by Matej
Link to comment
Share on other sites

How are you testing it? Are you uploading to a web server or just double-clicking on the file? It will probably work on a server, but your browser uses different permissions if you open a local file.

Link to comment
Share on other sites

in other words, you need to execute this within the context of a running web server. You can either set that up locally using an AMP stack targetted for your OS (WAMP - Windows, LAMP - Linux, MAMP - OSX) or get hosting (free, shared, etc) and test by uploading your files there. Locally developing will greatly increase your productivity and reduce development cycles, so it's definitely recommended and preferred to have a functioning local development environment.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...