Removed HTML Form
authorplehegar
Mon, 22 Aug 2011 17:29:04 -0400
changeset 12 a40ef7b52b60
parent 11 0a609628a5ab
child 13 20f5f73e9523
Removed HTML Form
Now sending application/json
src/main/resources/scripts/tracker.js
--- a/src/main/resources/scripts/tracker.js	Mon Aug 22 15:50:09 2011 -0400
+++ b/src/main/resources/scripts/tracker.js	Mon Aug 22 17:29:04 2011 -0400
@@ -1,67 +1,28 @@
-
-function buildMessage(data, boundary) {
-    var CRLF = "\r\n";
-    var parts = [];
-    var iter = 0;
-
-    while (iter < data.length) {
-        var part = "";
-        var type = "TEXT";
 
-        part = 'Content-Disposition: form-data; ';
-        part += 'name="' + data[iter][0] + '"' + CRLF + CRLF;
-
-        part += data[iter][1] + CRLF;
-
-        parts.push(part);
-        iter++;
-    }
-
-    var request = "--" + boundary + CRLF;
-        request+= parts.join("--" + boundary + CRLF);
-        request+= "--" + boundary + "--" + CRLF;
-
-    return request;
-}
 
 function send(user, data) {
-    var boundary = "AJAX-----------------------" + Date.now();
     var xhr = new XMLHttpRequest;
 
-    if (!("withCredentials" in xhr)) {
-      return false;
-    }    
-
     try {
     xhr.open("POST", "/user/"  + user + "/run", true);
-    xhr.setRequestHeader('X-HTML5TRACK', 'true');
     xhr.onreadystatechange = function() {
-        switch (xhr.readyState) {
-         case 4:
-            document.getElementById("log").textContent = "xhr done " + xhr.status + " " + xhr.statusText;
-            break;
-         case 0:
-            document.getElementById("log").textContent = "xhr unsent";
-            break;
-         case 1:
-            document.getElementById("log").textContent = "xhr opened";
-            break;
-         case 2:
-            document.getElementById("log").textContent = "xhr headers_received";
-            break;
-         case 3:
-            document.getElementById("log").textContent = "xhr loading";
-            break;
+        if (xhr.readyState == 4) {
+            if (xhr.status != 200) { // this needs to be 201
+              document.getElementById("log").textContent = "xhr failed? [" + xhr.status + "] " + xhr.statusText;
+            } else {
+              document.getElementById("log").textContent = "resource created at " + xhr.getResponseHeader("Location");
+            }
+        } else {
+            document.getElementById("log").textContent = "xhr readyState is " + xhr.readyState;
         }
     };
-    var contentType = "multipart/form-data; boundary=" + boundary;
-    xhr.setRequestHeader("Content-Type", contentType);
+    xhr.setRequestHeader("Content-Type", "application/json");
+    xhr.setRequestHeader("Content-Length", data.length);
 
-    var data = buildMessage([ ["user", user], ["gps_coords", data] ], boundary);
-
-    xhr.sendAsBinary(data);
+    xhr.send(data);
 
     } catch (e) {
+      document.getElementById("log").textContent = "Failed to send to server " + e;
       return false;
     }
    return true;
@@ -295,7 +256,7 @@
 }
 
 function stop() {
-  //  navigator.geolocation.clearWatch(watchId);
+  navigator.geolocation.clearWatch(watchId);
   running = false;
   clearInterval(intervalId);
   got_error = 0;
@@ -315,9 +276,7 @@
   }
   if (coords != null) {
     if (!send("teole",coords)) {      
-      document.getElementById("gps_coords").value = coords;
-      document.getElementById("xhr_failed").style.display = "inline";
-      document.getElementById("xhr_failed").disabled = false;
+      // do something!      
     }
   }
   coordinates = [];
@@ -329,6 +288,9 @@
   else
     document.visibilityState = true;
 
+  send("teole", "[4,5,6]");
+  return false;
+
   // @@ in the future, make sure this is empty
    if (storage) {
     window.localStorage.setItem("track_gps_app", "");
@@ -347,11 +309,6 @@
      document.getElementById("start").disabled = true;
    }
 
-    var xhr = new XMLHttpRequest;
-    if (!!xhr && !("withCredentials" in xhr)) {
-      document.getElementById("xhr_failed").style.display = "inline";
-      document.getElementById("xhr_failed").disabled = true;
-    }        
 }
 
 window.addEventListener("load", init, false);