将AJAX调用的结果转换为javascript变量

时间:2015-01-20 15:28:53

标签: javascript php jquery ajax

我一直试图找到解决方案一段时间,但我似乎无法让它工作。

我在做什么: 我想从我的php文件中获取一个数组,以便我可以在我的javascript中使用它。

使用example.php

$arr =  Array(
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(3,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(3,3,3,3,3,3,0,0,0,0,1,0,0,0,0,0,3,3,3,3,3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
            Array(0,3,3,3,3,3,3,0,0,0,1,0,0,0,3,3,3,3,3,0,0,0,0,0,3,3,0,1,1,1,1,1,1,1,0,0,0,0,0,0),
            Array(0,0,0,0,0,0,3,3,3,3,1,3,3,3,3,3,0,0,0,0,0,0,0,0,0,3,3,1,3,3,3,3,3,1,1,1,1,0,0,0),
            Array(0,0,0,0,0,0,0,3,3,3,1,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,3,1,3,3,3,3,3,3,3,3,1,0,0,0),
            Array(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,3,3,3,1,1,0,0),
            Array(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,3,3,1,1,1),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,3,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,3,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,3,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,3,3,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,3,3,3,3,3,3,3,3,3,3),
            Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3)
           );

echo json_encode($arr);

我用javascript函数调用它

function getArray(){
       $.ajax({
           url: 'Example.php',
           success: function(resultarray) {
                alert("success " + resultarray);
                return levelarray;
           },
           error: function(xhr, status, error) {
                //alert(xhr.responseText);
           }
      }); 
 }

这个alert("success " + resultarray);确实会返回数组。

但是当我想在我的javascript中使用它时,它一直说它是未定义的,或者它没有说什么。

我试过这样:

var test = getArray();
alert("result= "+ test);

因此,此警报不断返回错误或空值。

有谁知道我怎么能这样做?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

如果要在另一个js函数中使用成功结果,请将该函数放在ajax的success()中。 ajax运行异步,因此它不会按顺序与其他函数一起运行。做一些像:

$.ajax({
    url: 'Example.php',
    success: function(data) {
        // myFunction(), do something here with data from ajax;
    },
});

答案 1 :(得分:1)

这与javascript的异步性质有关。而不是像

那样使用你的功能
var test = getArray();

尝试一下

的内容
function getArray(){
   $.ajax({
       url: 'Example.php',
       success: function(resultarray) {
            alert("success " + resultarray);
            doSomethingWithMyTiles(resultarray);
       },
       error: function(xhr, status, error) {
            //alert(xhr.responseText);
       }
  }); 
 }

function doSomethingWithMyTiles(resultarray){
 //do stuff

}