Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将响应数据从ajax成功传递到回调函数?_Jquery - Fatal编程技术网

Jquery 如何将响应数据从ajax成功传递到回调函数?

Jquery 如何将响应数据从ajax成功传递到回调函数?,jquery,Jquery,这是我的ajax函数。我把这个函数称为 (function($) { $.fn.makeAjaxPostCall = function(options) { var settings = $.extend( true,{}, $.fn.makeAjaxPostCall.defaults, options ); $.ajax({ type: 'POST', ur

这是我的ajax函数。我把这个函数称为

(function($)
{

    $.fn.makeAjaxPostCall = function(options)
        {
            var settings = $.extend( true,{}, $.fn.makeAjaxPostCall.defaults, options );
            $.ajax({
                type: 'POST',
                url:  settings.url,
                data:   settings.data,
                success: function(data, textStatus, xhr){
                    if (xhr.readyState === 4 && xhr.status === 0) {

                        alert('Network Error');
                    }
                    else {alert(data);
                        settings.callback.call(data);
                    }
                },
                error: function(xhr, textStatus, errorThrown) {
                    if (textStatus !== null) {
                        alert("error: " + textStatus);
                    } else if (errorThrown !== null) {
                        alert("exception: " + errorThrown.message);
                    }
                    else {
                        alert ("error");
                    }
                }
            });
        };
        $.fn.makeAjaxPostCall.defaults = {
            type : 'POST',
            url : '',
            data : {},
            obj : $(document),
            callback : function(data){}
        };

})(jQuery);
帖子发生后,我从servlet返回“true”

$(document).makeAjaxPostCall({
    type : 'POST',
    url : 'UpdateNodeStatus',
    data : {
        workshopid : 1,
        userid : 1001,
        level : 5,
        nodeid : 10,
        resolutionid : 1
    },
    callback : function(data){
        alert(data);
    }
});
我在“成功”中得到的数据为“真”,但在回调中得到的数据为“未定义”


如何将“数据”从“success”传递到“callback”函数,以便在其中使用它?

您可以直接调用回调函数,而不必使用。调用方式如下所示

response.setContentType("text");
    PrintWriter pw = response.getWriter();
    pw.print(true);
或者,如果要使用
.call
,则将
作为第一个参数发送到
.call

settings.callback(data);

您可以直接调用回调函数,而不必使用。调用方法如下

response.setContentType("text");
    PrintWriter pw = response.getWriter();
    pw.print(true);
或者,如果要使用
.call
,则将
作为第一个参数发送到
.call

settings.callback(data);

不应该只是
设置。回调(数据)
?@Archer-使用
call()
也应该可以,但所有这些看起来都像是跳过了很多障碍,创建了一个新的ajax函数,它的功能与jQuery中已经提供的功能差不多?@Archer-是的,它正在工作。感谢you@adeneo我完全同意。不管怎样,为什么要扩展工作完美的东西?@adeneo-是的,我同意,但我不想每次调用ajax时都编写成功和失败的代码,在我的应用程序中,在不同的页面中进行近30到40次调用。它不应该仅仅是
settings.callback(data)
?@Archer-使用
call()
也应该可以,但所有这些看起来都像是跳过了很多障碍,创建了一个新的ajax函数,它的功能与jQuery中已经提供的功能差不多?@Archer-是的,它正在工作。感谢you@adeneo我完全同意。不管怎样,为什么要扩展工作完美的东西?@adeneo-是的,我同意,但我不想每次调用ajax时都编写成功和失败的代码,在我的应用程序中,在不同的页面中执行近30到40次。