0
我想回傳一個Ajax請求的結果給觸發函數。但是,我的回答是「迷失在路上」 ......如何將對象從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函數調用函數。
問題:
任何人都可以給我一個提示嗎?
謝謝!
編輯:
是這樣工作的:
// 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)
}
所以我hijqcking我最後一個參數傳遞的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傳遞,覆蓋將爲空。似乎工作正常。
謝謝。需要是異步的。讓我們嘗試回調。 – frequent
我想我有。在工作時發帖。謝謝! – frequent