匿名 js 函数,xhrpost dojo 不返回数据
anonymous js function with xhrpost dojo not returning data
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
相关文章:
- AJAX调用并在Node中获取返回数据
- 角度服务未返回数据
- 统计ajax返回数据中的html元素
- 有没有更好的方法可以测量从Ajax调用返回数据所需的总时间
- Imgur API图像搜索未返回数据
- javascript对象显示为null,但object.properties返回数据
- 如何从ajax调用JQuery返回数据
- 试图从嵌套的回调函数返回数据
- NodeJS FS 不从多个文件返回数据
- ajax函数未在C#中返回数据
- 如何在公共js模块中从web服务返回数据
- 将字符串与 JSON 对象组合以返回数据
- 嵌套异步函数未及时返回数据的问题
- NodeJS JSONStream 以一个长字符串的形式返回数据
- 如何使用 HTML 图像请求将数据发送到服务器,或返回数据作为响应
- 从 Angular 模态服务中的模态主体模板返回数据
- jQuery 每个循环返回数据两次
- $.getJSON 在返回数据后不会运行函数
- 如何访问返回数据中的特定json值
- 工厂返回数据后未设置角度$scope