什么是jQuery相当于Ajax.Request,以及onSuccess和onComplete?

时间:2013-01-24 00:01:35

标签: javascript jquery prototypejs

这是我的代码:

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函数,这就是我在思考的问题。

1 个答案:

答案 0 :(得分:4)

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

onSuccessonComplete类似功能的示例可能是这样的......

$.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函数,但最好避免使用它们,因为它们会对响应做出假设,并且可能导致意外失败。