gpt4 book ai didi

javascript - xhrpost dojo 的匿名 js 函数不返回数据

转载 作者:行者123 更新时间:2023-12-02 19:37:56 26 4
gpt4 key购买 nike

var cType = function(templateId){
dojo.xhrPost({
url : "/mediation1.0.1/template/getCollectorType",
handleAs : "text",
headers : {"Content-Type":"text/html"},
postData : templateId,
load: function(data){
return data;
}});
};

当我使用 cType(withSomeId) 调用此函数时,我得到了未定义的结果。即使当我获取局部变量并将数据分配给该变量时,返回该变量也没有帮助。

最佳答案

问题是您的 cType 函数没有返回任何内容。

var cType = function(templateId){
dojo.xhrPost({
url : "/mediation1.0.1/template/getCollectorType",
handleAs : "text",
headers : {"Content-Type":"text/html"},
postData : templateId,
load: function(data){
return data;
// this returns from the the load
// function, not the cType function!
}});

// You are not returning anything from the cType function.
};

您应该使用dojo.Deferred来完成您想要做的事情:

var cType = function(templateId){
var xhrArgs = {
url : "/mediation1.0.1/template/getCollectorType",
handleAs : "text",
headers : {"Content-Type":"text/html"},
postData : templateId
};

return dojo.xhrGet(xhrArgs);
};

var deferred = cType('templateId');
deferred.then(
function(data){
// do something with the data...
},
function(error){
// handle an error calling the server...
}
);

http://dojotoolkit.org/reference-guide/1.7/dojo/xhrGet.html (这有一个展示延迟技术的示例)

http://dojotoolkit.org/reference-guide/1.7/dojo/xhrPost.html

关于javascript - xhrpost dojo 的匿名 js 函数不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com