简体   繁体   English

jQuery ajax成功回调函数-如何使其匿名?

[英]jQuery ajax success callback function - how to make it anonymous?

I am unable to get the success callback to work in a jQuery ajax call. 我无法获得成功回调以在jQuery ajax调用中工作。 The following code calls interpretResponse() just fine, but of course resultJSON is undefined : 以下代码可以很好地调用interpretResponse() ,但当然resultJSONundefined

        var that = this;
        jQuery('#fsForm1492441').submit(function(event) {
            event.preventDefault();
            jQuery.ajax({ type: "POST",
                url: "format_result.php",
                data: jQuery(this).serialize(),
                success: that.interpretResponse(),
                dataType: "json"
            });
        });

        function interpretResponse(resultJSON) {
        // code here to handle resultJSON
        }

I want something like: 我想要类似的东西:

                success: function(resultJSON) { 
                         that.interpretResponse(resultJSON); 
                         },

How should the success callback be written? success回调应该如何编写?

just do this : 只是这样做:

success: interpretResponse,

your code will look like this - 您的代码将如下所示-

var that = this;
jQuery('#fsForm1492441').submit(function (event) {
    event.preventDefault();
    jQuery.ajax({
        type: "POST",
        url: "format_result.php",
        data: jQuery(this).serialize(),
        success: interpretResponse,
        dataType: "json"
    });
});

function interpretResponse(resultJSON) {
    // code here to handle resultJSON
}

The answer above was correct, but it turned out that I was dealing with a different problem. 上面的答案是正确的,但是事实证明我正在处理另一个问题。 The weird bug in FireFox that keeps ajax calls from getting past readyState 1 was causing the callback function to not load. FireFox中的怪异错误使Ajax调用无法通过readyState 1,这导致回调函数无法加载。 I ended up using the workaround described here: Ajax won't get past readyState 1, why? 我最终使用了这里描述的解决方法: Ajax无法通过readyState 1,为什么? Basically getting Firefox to set the callback onload rather than onreadystatechange . 基本上是让Firefox设置回调onload而不是onreadystatechange

Thanks to pXL for the answer to the question the way I asked it. 感谢pXL以我的方式回答问题。

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

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