pythonic
pythonic

Reputation: 21655

How do I test an AJAX based code locally on my PC?

I am learning web development. Currently, I am learning AJAX. I am trying to run an example code, where the server side would return back text read from a file residing on the server. The example function is given below.

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
     myHeading.textContent = xhttp.responseText;
    }
  };
  xhttp.open("GET", "some_file_on_the_server.txt", true);
  xhttp.send();
} 

Now, my question is, how can I check this code locally on my PC, and where would I need to place some_file_on_the_server.txt.

Upvotes: 1

Views: 370

Answers (1)

Brad
Brad

Reputation: 163468

You'll need an HTTP server of some kind to serve up that file, if you want to properly use AJAX.

These days, I prefer to use a Vagrant box. Vagrant is a tool for setting up development environments, usually on a local VM. You add one file to your code, and then other developers can have the same development environment as you. There are pre-configured boxes, and it's trivial to add a bit of extra provisioning script to set up a web server how you want it.

Upvotes: 1

Related Questions