如何将一个对象从ajax成功处理程序传递回调用函数? [英] how to pass back an object from an ajax success handler to the calling function?
问题描述
我正在尝试将Ajax请求的结果传递回触发函数.但是我的回答是迷路了" ...
I'm trying to pass back the results of an Ajax request to the triggering function. But my response is "lost on the way"...
这就是我在做什么:
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 () {}
});
}
一切正常,但是我不知道如何通过getItems函数将objRespone
从Ajax成功处理程序传递回调用函数.
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.
问题:
谁能给我提示吗?
Question:
Can anyone give me a hint?
谢谢!
像这样工作:
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 )
}
所以我要让我的最后一个参数传递cbk
而不是get/post
So I'm hijqcking my last parameter to pass cbk
instead of 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将为null.似乎工作正常.
So, if get/post are passed, override will be null. Seems to work fine.
推荐答案
由于您的ajax调用是异步的,因此getItems
函数的执行将在您取回数据之前很长时间完成.您可以做的一件事是在调用中添加一个回调参数,并在ajax成功时调用它.像这样:
Since your ajax call is asynchronous, the execution of the getItems
function will be finished long before you get your data back. One thing you could do is add a callback argument to the call, and call it when your ajax succeeds. Something like:
getItems: function(cbk)
//(...)
successHandler = function(objResponse) {
// here is the objResponse I need to pass back
cbk(ojbResponse);
};
但是随后,您必须将dynoData
调用更改为getItems
以添加回调,这将完成新对象的设置.
but then, you have to change your dynoData
call to getItems
to add the callback, that will finish the new object's setup.
另一种解决方案是使ajax调用同步,但这会暂停浏览器,直到您恢复数据,这使系统无响应.
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成功处理程序传递回调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!