匿名js函数与xhrpost dojo不返回数据 [英] anonymous js function with xhrpost dojo not returning data

查看:142
本文介绍了匿名js函数与xhrpost dojo不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)调用这个函数时,我得到undefined。
即使当我使用本地变量并将数据分配给该变量时,返回该变量也没有帮助。

When I call this function with cType(withSomeId), I get undefined. Even When I take local variable and assign data to that variable, returning thatvariable also not helping.

推荐答案

是你的cType函数不返回任何东西。

The problem is that your cType function does not return anything.

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 完成你要做的事情:

You should be using dojo.Deferred to accomplish what you are trying to do:

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/xhrGet.html (This has an example that shows the deferred technique)

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

这篇关于匿名js函数与xhrpost dojo不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆