简体   繁体   English

jQuery ajaxSuccess()事件处理程序

[英]jQuery ajaxSuccess() event handler

there is one thing I really don't like jQuery is its chaining. 我真的不喜欢jQuery的一件事就是它的链接。 and all of the samples I found are using this. 我发现的所有样本都在使用此方法。 So is there a way to define ajaxSuccess not using chaining? 那么有没有一种方法可以不使用链接来定义ajaxSuccess? I have tried this but it doesn't work. 我已经尝试过了,但是没有用。

jQuery.ajax({
    dataType: "json",
    url: this.DIRECTORY_PHP,
    data: data,
    success: this.handleSuccessEvent
});

A general note : "chaining" can always be broken in a succession of disctinct calls : 一般说明:在一系列不同的调用中,“链接”总是可以被破坏的:

$.ajax({ ... }).done(function1).done(function2)
// can be split into :
var ajaxReq = $.ajax({ ... });
ajaxReq.done(function1);
ajaxReq.done(function2);

If only handleSuccessEvent would have a meaning in the context ( this ), your example would work. 如果只有handleSuccessEvent在上下文( this )中具有含义,那么您的示例将起作用。 Replace it with an existing and declared function name and ( as in documentation ) it will work. 将其替换为现有的已声明函数名称,并且( 如文档中所示 )它将起作用。

var myFunction = function(data){ /* TODO */ };
jQuery.ajax({
    dataType: "json",
    url: this.DIRECTORY_PHP,
    data: data,
    success: myFunction
});

Assignment of an anonymous function to a variable is totally optional and was written to showcase the flexibility of that syntax. 将匿名函数分配给变量完全是可选的,其编写目的是展示该语法的灵活性。 Feel free to follow this approach as well: 也可以随意采用这种方法:

function myFunction(data){
/* TODO */
}

jQuery.ajax({
    dataType: "json",
    url: this.DIRECTORY_PHP,
    data: data,
    success: myFunction
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM