【发布时间】:2013-06-06 03:18:45
【问题描述】:
我正在调用一个函数,它将ajax GET 发送到一个 url,如下所示:
// parameters = url, callback, boolean
that.mapUrl( window.location.search, function(spec) {
console.log("initial mapping done");
console.log(spec);
// do stuff
}, true);
mapUrl 将触发 Ajax 请求。在 Ajax done 或 success 处理程序中,我想触发我的回调函数,但这样做:
$.ajax({
method: 'GET',
url: obj[1],
context: $('body')
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log("FAILED");
configuration = {
"errorThrown":errorThrown,
"textStatus": textStatus,
"jqXHR": jqXHR
}
}).done(function(value, textStatus, jqXHR) {
console.log("OK");
console.log(callback) // undefined!
configuration = {
"value":value,
"textStatus": textStatus,
"jqXHR": jqXHR
}
});
问题:
所以我想知道如何将我的回调函数传递给ajaxdone-callback。知道怎么做吗?
谢谢!
编辑
这是完整的mapURL 函数
that.mapUrl = function (spec, callback, internal) {
var key,
obj,
parsedJSON,
configuration = {"root" : window.location.href};
if (spec !== undefined && spec !== "") {
obj = spec.slice(1).split("=");
key = obj[0];
console.log(key);
switch (key) {
case "file":
$.ajax({
method: 'GET',
url: obj[1],
context: $('body')
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log("FAILED");
configuration = {
"errorThrown":errorThrown,
"textStatus": textStatus,
"jqXHR": jqXHR
}
}).done(function(value, textStatus, jqXHR) {
console.log("OK");
configuration = {
"value":value,
"textStatus": textStatus,
"jqXHR": jqXHR
}
});
break;
default:
// type not allowed, ignore
configuration.src = [];
break;
}
}
return configuration;
};
【问题讨论】:
-
能否分享方法声明`mapUrl
-
一秒钟,即将到来
-
你调用的方法?不确定是什么问题。显示您尝试过的内容。
-
callbackcallback是否有实际的传递方法? -
是的,我正在调用
myplugin.mapUrl(window.location.href, function (d) {}, false);我也得到了 OK 控制台,但我无法访问回调。
标签: javascript jquery ajax callback promise