如何以核心方式返回对象值

时间:2015-06-16 13:32:42

标签: javascript object return

我试图返回一个全局变量并在数组中设置一个对象。

有人能解释我在检索全局变量outPutArray时做错了什么吗?当我试图返回时它变得不确定。



var outPutArray = {};
var goAjax = function(data, filePath) {
  data = $(this).serialize() + "&" + $.param(data);
  $.ajax({
    type: "POST",
    dataType: "json",
    url: filePath, //Relative or absolute path to response.php file
    data: data,
    success: function(data) {
      for (x in data) {
        outPutArray[x] = data[x];
      }
      //logs the the wanted value
      console.log('inside ' + outPutArray['json']);
    }
  });
};

goAjax.prototype.getValue = function() {
  console.log('outside ' + outPutArray['json']);
  //logs undefined ??
};




2 个答案:

答案 0 :(得分:0)

当你尝试返回它时,它是未定义的,因为AJAX是异步的。

请参阅this questionsthis question,或Google"返回AJAX响应"或类似的东西。

归结为异步调用发生在正常事件循环和执行之外的事实,因此当您使用异步响应设置全局变量时,JavaScript无法知道"知道&#34 34;当它到达那里时。

答案 1 :(得分:0)

首先,我要感谢每一个试图帮助我的人。答案是使用一个参数作为java中的接口,在另一个函数中触发函数或在其他方法中触发的方法。 / p>



 /**
   *This is a constructor of javascript firing a server side PHP script.
   * 
 * @param {Object} filePath PHP file path
   */
 var AjsToPHP = function(filePath){
 this.filePath = filePath;
   };
      
       
 /**
  * 
  * Firing server side PHP script and return respond as an array.
 * @param {Object} data Input an array of object to go as POST command.
 * @param {Object} response An interface function to return the respond value.
  */
  AjsToPHP.prototype.PHP_PROCESS_RETURN = function(data,response){
  	 var PHP_RETURNS= {}; 
    data = $(this).serialize() + "&" + $.param(data);

  $.ajax({
      type: "POST",
      dataType: "json",
      url: this.filePath, //Relative or absolute path to response.php file
      data: data,
      success: function(data) {
      for (x in data) {
       PHP_RETURNS[x] = data[x];
 
        }
          response(PHP_RETURNS);

    
      }
    });
  };



/**
 * 
 * Firing server side PHP script and return respond OK if it success.
 * @param {Object} data Input an array of object to go as POST command.
 * @param {Object} response An interface function to return the respond value.
 */
 AjsToPHP.prototype.PHP_PROCESS = function(data,response){
  	 var PHP_RETURNS= ""; 
    data = $(this).serialize() + "&" + $.param(data);

  $.ajax({
      type: "POST",
      dataType: "json",
      url: this.filePath, //Relative or absolute path to response.php file
      data: data,
      success: function(data) {     
       PHP_RETURNS= "ok";     
          response(PHP_RETURNS);

      }
    });
 };
   




一个例子



<script src="jsToPHP.js"></script>  
<script src="js/jquery-1.8.2.min.js"></script>
$("document").ready(function(){


        var data = {
      "action": "test",
      "Name" :"abdulla"
        };



var obj = new AjsToPHP("response.php");

obj.PHP_PROCESS_RETURN(data,function(PHP_RESPONE){	
	alert('inside '+PHP_RESPONE['json']);
});


obj.PHP_PROCESS(data,function(PHP_RESPONE){
	
	alert(PHP_RESPONE);
	
	
});
&#13;
&#13;
&#13;

response.php脚本

&#13;
&#13;
<?php



function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}



if (is_ajax()) {
    
       
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
  
    switch($action) { //This is how we are able to create different function according to different function call
      case "test": test_function(); break;
    }
  }
  
  
  
  
}



function test_function(){
 

 //this a correct way of creating an associative array.
  $name = $_POST["Name"];
  $return["name"] = $name;
  $return["favorite_beverage"] = "Coke";
  $return["favorite_restaurant"] = "McDonald's";  
  $return["json"] = json_encode($return); 
  
  //return data to javascript
  echo json_encode($return); 
}













?>
&#13;
&#13;
&#13;