Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从我的javascript函数中获取值_Javascript_Jquery_Ajax_Angularjs - Fatal编程技术网

无法从我的javascript函数中获取值

无法从我的javascript函数中获取值,javascript,jquery,ajax,angularjs,Javascript,Jquery,Ajax,Angularjs,首先,下面是我的代码。我的主要问题是,在调用或ajax调用之后,无法在invokeConfigFile函数之外返回结果数据 var invokeConfigFile = function() { return $http.get('json/config.json'); }; var data = {} var getDataFromInvokedFile = function() { invokeConfigFile().then(function(result) { v

首先,下面是我的代码。我的主要问题是,在调用或ajax调用之后,无法在invokeConfigFile函数之外返回结果数据

var invokeConfigFile = function() {
  return $http.get('json/config.json');
};

var data = {}

var getDataFromInvokedFile = function() {
  invokeConfigFile().then(function(result) {

    var current_env = result.data.current_environment,
        list_env = result.data.environments;

    angular.forEach(list_env, function(value, key) {
      // retrive the config on the current environment
      if(key === current_env) {

          data = value;
          return false; //let's stop if the condition are met
      }
    });
    console.log(data); // return data GOOD
    return data;


  }, function(result) {

    if(result.status == 404) {
      alert("Server config.js file not found.");
      return false;
    }
  });
  console.log(data); // return data EMPTY
};

var my_urls = getDataFromInvokedFile();
我在这里试图完成的是,在我从ajax调用invokeConfigFile()获得结果数据之后,我将把它存储到一个对象中

像这样的

return {
 my_obj : getDataFromInvokedFile()
}

我做了一些更改,所以评论“检查这里”

} console.log(数据);//检查这里

var getDataFromInvokedFile = function() {
  invokeConfigFile().then(function(result) {

    var current_env = result.data.current_environment,
        list_env = result.data.environments;

    angular.forEach(list_env, function(value, key) {
      // retrive the config on the current environment
      if(key === current_env) {    
          data = value;
          return false; //let's stop if the condition are met
      }
    });
    console.log(data); // return data GOOD
    return data;   

  }, function(result) {

    if(result.status == 404) {
      alert("Server config.js file not found.");
      return false;
  });      
};

您不能从函数返回数据,因为AJAX调用是异步的。响应在函数返回后到达

使用回调在数据到达时处理数据:

function getDataFromInvokedFile(callback) {
  invokeConfigFile().then(function(result) {

    var current_env = result.data.current_environment,
        list_env = result.data.environments;

    angular.forEach(list_env, function(value, key) {
      // retrive the config on the current environment
      if(key === current_env) {

          data = value;
          return false; //let's stop if the condition are met
      }
    });
    callback(data);


  }, function(result) {

    if(result.status == 404) {
      alert("Server config.js file not found.");
      calback(false);
    }
  });
}
用法:

getDataFromInvokedFile(function(data){
  // here you have the data
});

您添加的代码将永远不会执行,因为退出函数之前,行中的
return
语句将不会执行。问题是Ajax调用将是异步的。这意味着最终的
console.log(数据)将在调用结束之前运行。您应该将其放在回调函数中,以确保设置了值,或者使Ajax调用同步。但是对于最后一部分,我如何在
返回{my_obj:getDataFromInvokedFile()}
中设置返回的数据呢?谢谢,它可以工作。但是如何在returnobj属性中设置数据呢。例如
getDataFromInvokedFile(函数(数据){data=data;});返回{my_data:data}
。因此,当我调用/调用该文件时,它只是
my_invoked_file.my_data
@user2720708:情况也是如此,您不能使用调用
getDataFromInvokedFile
后的代码中的数据,因为该代码是在调用回调函数之前执行的。使用异步调用时,必须异步处理结果。你不能在数据存在之前返回它。你能帮我使它同步吗?我很难做到这一点_T@user2720708:据我所知,
$http.get
方法似乎不支持同步请求。您可以使用其他库,但使用同步请求不是一个好主意,因为它会在等待响应时完全冻结浏览器。嗯,我想我将使用$rootScope传递数据。嗯,想不出其他简单的方法。