这是我的代码:
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
函数,这就是我在思考的问题。
答案 0 :(得分:4)
一个这样的功能是.ajax
。文档非常详尽:jQuery .ajax function。
onSuccess
和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
函数,但最好避免使用它们,因为它们会对响应做出假设,并且可能导致意外失败。