0

我正在尝试将 Ajax 请求的结果传递回触发函数。但我的回答是“迷路了”......

这就是我正在做的事情:

 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 () {}
    });
}

一切正常,但我不知道如何objRespone通过 getItems 函数从 Ajax 成功处理程序传回调用函数。

问题:
谁能给我一个提示?

谢谢!

编辑:
像这样工作:

// 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 )
}

所以我在劫持我的最后一个参数来传递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 )

因此,如果通过了 get/post,override 将为空。似乎工作正常。

4

1 回答 1

1

由于您的 ajax 调用是异步的,因此getItems函数的执行将在您取回数据之前很久就完成。您可以做的一件事是在调用中添加一个回调参数,并在您的 ajax 成功时调用它。就像是:

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

但是,您必须更改dynoData调用以getItems添加回调,这将完成新对象的设置。

另一种解决方案是使 ajax 调用同步,但这会暂停浏览器,直到您取回数据,这会使系统无响应。

于 2012-12-10T22:31:19.780 回答