1

这是我的代码:

this.ajax = new Ajax.Request(this.url, {
  method: 'get',
  parameters: { 'timestamp' : this.timestamp },
  onSuccess: function(transport) {
    // handle the server response
    var response = transport.responseText.evalJSON();
    this.comet.timestamp = response['timestamp'];
    this.comet.handleResponse(response);
    this.comet.noerror = true;
  },
  onComplete: function(transport) {
    // send a new ajax request when this request is finished
    if (!this.comet.noerror)
      // if a connection problem occurs, try to reconnect each 5 seconds
      setTimeout(function(){ comet.connect() }, 5000); 
    else
      this.comet.connect();
    this.comet.noerror = false;
  }
});

我主要是想了解onComplete功能,这就是我在思考的问题。

4

1 回答 1

4

一个这样的功能是.ajax。文档非常详尽:jQuery .ajax function

onSuccess和-like 功能的一个例子onComplete可能是这样的......

$.ajax({
    url: "test.php",
    type: "post",
    data: values,
    success: function() {
        alert("success");
    },
    error: function() {
        alert("failure");
    },
    complete: function() {
        alert("both success and error have been checked, request finished!");
    }
});

还有个人.post.get功能,但最好避免使用它们,因为它们会对响应做出假设,这可能导致意外失败。

于 2013-01-24T00:03:54.853 回答