$.ajax() callbacks not being executed
I am trying to execute slightly more than "Hello, world" using .ajax() specifically and with both a success and error callback. I have:
- var MeasureClockSkew = function()
- {
- var that = this;
- var lastButtonPress = new Date().getTime();
- var registerError = function(XMLHttpRequest, textStatus, errorThrown)
- {
- alert("registerError called.");
- $("#results").append($("li").addClass("error").append("Error: " + textStatus));
- $("#button").attr("disabled", "false");
- };
- var registerSuccess = function(data, textStatus, XMLHttpRequest)
- {
- alert("registerSuccess called.");
- try
- {
- var remote = JSON.parse(data).time();
- var halfway = (lastButtonPress + new Date().getTime()) / 2;
- var skew = (remote - halfway) / 1000;
- $("#results").append($("li").addClass("success").append("Estimated clock skew: " + skew));
- }
- catch(error)
- {
- $("#results").append($("li").addClass("error").append("Error parsing JSON."));
- }
- $("#button").attr("disabled", "false");
- };
- var buttonPress = function()
- {
- alert("Begun buttonPress.");
- lastButtonPress = new Date().getTime();
- $("#button").attr("disabled", "true");
- $.ajax(data = "", dataType = "json", error = this.registerError, success = this.registerSuccess, type = "POST", url = "/time/json");
- alert("Finished buttonPress.");
- };
- return {
- buttonPress: buttonPress
- }
- } ( );
- $("#button").click(MeasureClockSkew.buttonPress);
When I click on #button, it gives the "Begun buttonPress." and "Finished buttonPress." alerts, but not yet the alerts from the callbacks. Whether I specify
-
$.ajax(data = "", dataType = "json", error = this.registerError, success = this.registerSuccess, type = "POST", url = "/time/json")
or:
-
$.ajax(data = "", dataType = "json", error = registerError, success = registerSuccess, type = "POST", url = "/time/json")
or other permutations, I don't yet have proof of concept that the callbacks have been called at all.
/time/json returns a value like '{"time": 1273522247.674603}' as application/json; when it was not debugged and gave an error page, it didn't get the error callback.
Suggestions for what I should be doing differently? I'd really like to know how to use $.ajax() like this even if I usually use something higher-level.
Jonathan Hayward