title: Ajax write to server issue
body: <p>I have a very unique (and very in-depth) problem. If either of those two characteristics don't appeal to you, please don't feel the need to read on.</p>
  
  <p>I have a program that I have written from scratch. It is creating an HTML page, allowing a user to insert data into that page, and then submitting that data.<br>
  Upon submit, the data writes to a node.js server I am running locally on my machine. Its only job is to take the data passed to it, and print it to the console that started it (a cmd prompt in this case)<br>
  It writes via a querystring in the URL, i.e. <code>http://localhost/?&lt;parametersGoHere&gt;</code> <br>
  The client files (HTML page, javascript program) are also being hosted using a node.js module called http-server. Link to the module: <a href="https://github.com/nodeapps/http-server" rel="nofollow">https://github.com/nodeapps/http-server</a><br>
  (Example: going into the directory where all of my files are stored, and running the command "http-server" hosts all of my files on <code>localhost:8080</code>)<br></p>
  
  <p>This is my issue: when writing to the node.js server, the ajax call fails every single time.<br>
  I can physically write out a URL that includes parameters with values, and the server will spit out the correct data every time.</p>
  
  <p>I've looked at this in google chrome's developer tools, and can see it's having issues contacting the address I list. But the problem is, <em>it's the exact same url as the one I can manually type in and get results with.</em><br>
  That being said, the error (in my opinion) is somewhere in the network.js file. Can anyone help me find this error?</p>
  
  <p><br><br>
  Server: (ran with node server.js in its directory on a command prompt)</p>
  
  <pre><code>var http = require('http');
  var querystring = require('querystring');
  var url = require('url');
  
  var handler = function(request, response) {
    var params = querystring.parse(url.parse(request.url).query);
  
    var writeOut = function(status) {
      response.writeHead(status, {'Content-Type': 'text/plain'});
      response.end();
    }
  
    var name = params.name;
    var workDesc = params.workDesc;
    var wLoc = params.wLoc;
    var DOC = params.DOC;
    var severity = params.severity;
      console.log("Name: " + name);
      console.log("Work Description: " + workDesc)
      console.log("Location: " + wLoc)
      console.log("Date of Creation: " + DOC)
      console.log("Severity: " + severity);
      console.log("");
      writeOut(200);
  }
  
  var server = http.createServer(handler);
  server.listen(80);
  </code></pre>
  
  <p><br>
  Scripts running on the HTML page:</p>
  
  <pre><code>var load = function()
  {
      getDate();
      getLocation();
  }
  
  var verifyWords = function (ev) {
      var text = document.getElementById("workDesc").value;
      if (text.split(" ").length &gt; 299)
          document.getElementById("workDesc").style.borderColor = "red";
      //return true;
  };
  
  var getDate = function () {
      var updateItem = document.getElementById("date");
      var today = new Date();
      var day = today.getDate();
      var month = today.getMonth() + 1;
  
      var year = today.getFullYear();
  
      if (day &lt; 10)
          day = '0' + day;
      if (month &lt; 10)
          month = '0' + month;
  
      today = year + '-' + month + '-' + day;
      updateItem.innerHTML = today;
  
  };
  
  var getLocation = function () {
      var updateItem = document.getElementById("location");
  
      var locationInfo = function (position) {
          var lat = position.coords.latitude;
          var lon = position.coords.longitude;
          var alt = position.coords.altitude;
          var info = "Latitude: " + lat + "&lt;br /&gt;" + "Longitude: " + lon + "&lt;br /&gt;" + " Altitude: " + alt;
          var btn = document.getElementById("geoBtn");
          updateItem.innerHTML = info;
          updateItem.appendChild(btn);
      }
  
      var locationError = function (error) {
          var errMsg = ['',
          'Permission denied',
          'Position unavailable',
          'timeout'];
  
          updateItem.value = ("Error receiving location info: " +
          errMsg[error.code]);
          updateItem.style.Color = "rgba(255,0,0,.4)";
      }
  
      var options = {
          enableHighAccuracy: true,
          timeout: 10000,
          maximumAge: 30000
      };
  
      var watchId = navigator.geolocation.getCurrentPosition(
          locationInfo, locationError);
  
  };
  
  var submitted = function (form) {
      var loc = document.getElementById("location");
      var severity = document.getElementById("severity");
      var desc = document.getElementById("workDesc");
      var good = true;
      if (document.getElementById("name").value.length &lt; 3) {
          document.getElementById("name").style.backgroundColor = "rgba(255,0,0,.3)";
          good = false;
      }
      if (desc.value.split(" ").length &gt; 299) {
          desc.style.borderColor = "red";
          good = false;
      }
      if (desc.value.length &lt; 2) {
          workDesc.style.backgroundColor = "rgba(255,0,0,.3)";
          good = false;
      }
      if (!(loc.innerHTML.length &gt; 0 &amp;&amp; loc.innerHTML.indexOf("Error") === -1 &amp;&amp; loc.innerHTML.indexOf("Lat") &gt;= 0)) {
          loc.style.backgroundColor = "rgba(255,0,0,.3)";
          good = false;
      } else if (severity.value === "default") {
          severity.style.backgroundColor = "rgba(255,0,0,.3)";
          good = false;
      }
      if (good) {
          startSubmitData();
      }
  };
  
  var startSubmitData = function () {
      try {
          if (submitData() === true)
              return true;
      }
      catch (err)
      {
          saveLocal();
      }
  };
  
  var clear = function()
  {
      var nam = document.getElementById("name");
      nam.style.backgroundColor = "white";
      var dat = document.getElementById("date");
      dat.style.backgroundColor = "white";
      var loc = document.getElementById("location");
      loc.style.backgroundColor = "white";
      var sev = document.getElementById("severity");
      sev.style.backgroundColor = "white";
      var desc = document.getElementById("workDesc");
      desc.style.backgroundColor = "white";
      dat.innerHTML = desc.value = "";
      var btn = document.getElementById("geoBtn");
      loc.innerHTML = "";
      loc.appendChild(btn);
      sev.selectedIndex = 0;
      getDate();
  }
  var saveLocal = function () {
      try {
          var nam = document.getElementById("name");
          var dat = document.getElementById("date");
          var loc = document.getElementById("location");
          var btn = document.getElementById("geoBtn");
          loc.removeChild(btn);
          var sev = document.getElementById("severity");
          var desc = document.getElementById("workDesc");
          alert("local");//Do localstorage
          var i = 0;
          while (localstorage["name" + i].length &gt; 0)
              i++;
          localStorage["name" + i] = nam.value;
          localStorage["date" + i] = dat.innerHTML;
          localStorage["sev" + i] = sev.value;
          localStorage["desc" + i] = desc.value;
          localStorage["locaton" + i] = loc.innerHTML;
          clear();
      }
      catch (err) {
          alert("Localstorage is not defined. Is this running from file:///?");
      }
  };
  </code></pre>
  
  <p><br>
  HTML Page:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" manifest="cache.appcache"&gt;
  &lt;head&gt;
      &lt;title&gt;Create Work Order&lt;/title&gt;
      &lt;meta name="viewport" content="width=1060"&gt;
      &lt;script src="project.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="network.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="jquery-2.0.3.js"&gt;&lt;/script&gt;
      &lt;script&gt;
          function eval(evt) {
              var theEvent = evt || window.event;
              var key = theEvent.keyCode || theEvent.which;
              key = String.fromCharCode(key);
              var regex = /[a-zA-Z]/;
              if (!regex.test(key)) {
                  theEvent.returnValue = false;
                  if (theEvent.preventDefault) theEvent.preventDefault();
              }
          }&lt;/script&gt;
      &lt;link rel="stylesheet" href="project.css" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;header id="title"&gt;
          &lt;h2 class="title2"&gt;Create Work Order&lt;/h2&gt;
  &lt;/header&gt;
  &lt;body onload="load()"&gt;
      &lt;div&gt;
          &lt;section id="workForm"&gt;
              &lt;label&gt;First name:&lt;/label&gt;
              &lt;input type="text" id="name" style="width:20%" onkeypress="eval(event)" /&gt;
              &lt;Label&gt;Last name:&lt;/label&gt;
              &lt;input type="text" id="lastName" style="width: 20%" onkeypress="eval(event)"/&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;label&gt;Work Description:&lt;/label&gt;
              &lt;br /&gt;
              &lt;textarea id="workDesc" placeholder="Enter up to 300 words here." onkeyup="verifyWords(event)" maxlength="3000"&gt;&lt;/textarea&gt;
              &lt;br /&gt;
              &lt;label&gt;Work Location:&lt;/label&gt;
              &lt;p id="location"&gt;
                  &lt;input type="button" value="Refresh Location" id="geoBtn" onclick="getLocation()" style="float:right; margin-right:50%; height:3em;" /&gt;
              &lt;/p&gt;
              &lt;br /&gt;
              &lt;label&gt;Date of creation:&lt;/label&gt;
              &lt;p id="date"&gt;&lt;/p&gt;
              &lt;br /&gt;
              &lt;label&gt;Severity:&lt;/label&gt;
              &lt;br /&gt;
              &lt;select id="severity" name="severity" style="margin-top:5px;"&gt;
                  &lt;option value="default"&gt;Select One&lt;/option&gt;
                  &lt;option value="MINOR"&gt;Minor&lt;/option&gt;
                  &lt;option value="MAJOR"&gt;Major&lt;/option&gt;
                  &lt;option value="URGENT"&gt;Urgent&lt;/option&gt;
              &lt;/select&gt;
              &lt;br /&gt;
              &lt;input id="submit" type="button" value="Submit" onclick="submitted(event)"/&gt;
          &lt;/section&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><br>
  Lastly, my network.js code (called when submit occurs):</p>
  
  <pre><code>var submitData = function () {
      var loc = document.getElementById("location").innerHTML;
      var ind = loc.indexOf("&lt;input");
      var loc = loc.substring(0, ind);
      //var dataString = "name=" + document.getElementById("name").value + "&amp;workDesc=" + workDesc.value + "&amp;DOC=" + date.innerHTML + "&amp;severity=" + severity.value + "&amp;wLoc=" + loc;
      var dataString = "name=Joe&amp;workDesc=Test&amp;DOC=8-5-2013&amp;severity=MAJOR&amp;wLoc=Latitude: 23234 Longitude:-239823 Altitude:Unavailable";
      alert(dataString);
      $.ajax({
          type: "GET",
          url: "http://localhost/",
          data: dataString,
          success: function () {
              clear();
          },
          error: function()
          {
              saveLocal();
              alert("Could not connect to server.");
          }
      });
      submitLocal();
      return true;
  }
  
  var submitLocal = function()
  {
  }; 
  </code></pre>
  
  <p><br><br>
  I realize this is a lot of code to read through. Please let me know if I can make this easier to read in any way, or easier to understand, by leaving me a comment.</p>