我正在尝试将 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 将为空。似乎工作正常。