简体   繁体   English

如何将对象从ajax成功处理程序传递回调用函数?

[英]how to pass back an object from an ajax success handler to the calling function?

I'm trying to pass back the results of an Ajax request to the triggering function. 我试图将Ajax请求的结果传递回触发函数。 But my response is "lost on the way"... 但是我的回答是“迷路了”。

This is what I'm doing: 这就是我在做什么:

 some = function (){
     // inside some function - this will call getItems()
     content = $.parseJSON( dynoData[ dyn.method ]() );
 };

...
dynoData = {
    getItems : function(){

        var form = "",
        service = "../services/pull_items.cfc",
        method = "process",
        returnformat = "JSON",
        targetUrl = "",
        formdata = "form=getItems&method="+method+"&returnformat="+returnformat,
        successHandler = function(objResponse) {
            // here is the objResponse I need to pass back
            console.log( objResponse );
            return ( objResponse );
        };

    ajaxFormSubmit( form, 
                    service, 
                    formdata, 
                    targetUrl, 
                    successHandler, 
                    "yes", 
                    "", 
                    returnformat, 
                    "" );
    }


ajaxFormSubmit = function ( form, 
                            service, 
                            formdata, 
                            targetUrl, 
                            successHandler, 
                            dataHandler, 
                            errorHandler, 
                            returnformat, 
                            type ){     
    $.ajax({
        async: false,
        type: type == "" ? "get" : type,
        url: service,
        data: formdata,
        dataType: returnformat,
        success: function( objResponse ){
            if (objResponse.SUCCESS == true || 
                    typeof objResponse == "string" ){
                dataHandler == "yes" ? 
                    successHandler( objResponse ) : successHandler();   
            }
        },  
        error: function () {}
    });
}

Everything works correctly, but I don't know how to pass back objRespone from the Ajax successhandler via the getItems function to the call function. 一切正常,但是我不知道如何通过getItems函数将objRespone从Ajax成功处理程序传递回调用函数。

Question: 题:
Can anyone give me a hint? 谁能给我一个提示?

Thanks! 谢谢!

EDIT: 编辑:
Works like this: 像这样工作:

// inside some function
var content,
    cbk = function(objResponse){
        content = objResponse;
        };

// get dynamic data
$.parseJSON( dynoData[ dyn.method ](cbk) );

console.log( content );
}

// inside getItems
getRetailers : function(cbk){
    ...
    successHandler = function(objResponse, cbk) {
        cbk( objResponse );
        };
ajaxFormSubmit( form, service, formdata, targetUrl, successHandler, "yes", "", returnformat, cbk )
}

So I'm hijqcking my last parameter to pass cbk instead of get/post 所以我在给我的最后一个参数传递cbk而不是get/post

var ajaxFormSubmit = 
    function ( form, service, formdata, targetUrl, successHandler, dataHandler, errorHandler, returnformat, type ){
    // cleanup
    var override = null;

    if ( type !== "" && type !== "post" ){
        override = type;
        type = "get";
    }

    ... inside AJAX Successhandler
    dataHandler == "yes" ? successHandler( objResponse, override ) : successHandler( override )

So, if get/post are passed, override will be null. 因此,如果传递了get / post,则override将为null。 Seems to work fine. 似乎工作正常。

Since your ajax call is asynchronous, the execution of the getItems function will be finished long before you get your data back. 由于您的ajax调用是异步的,因此getItems函数的执行将在您取回数据之前很长时间完成。 One thing you could do is add a callback argument to the call, and call it when your ajax succeeds. 您可以做的一件事是在调用中添加一个回调参数,并在ajax成功时调用它。 Something like: 就像是:

getItems: function(cbk)
//(...)
    successHandler = function(objResponse) {
        // here is the objResponse I need to pass back
        cbk(ojbResponse);
    };

but then, you have to change your dynoData call to getItems to add the callback, that will finish the new object's setup. 但是随后,您必须将dynoData调用更改为getItems以添加回调,这将完成新对象的设置。

Another solution would be to make the ajax call synchronous, but this halts your browser until you get your data back, which makes the system unresponsive. 另一个解决方案是使ajax调用同步,但这会暂停浏览器,直到您取回数据,这使系统无响应。

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

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