隐藏echo json_encode的输出

时间:2016-04-04 23:22:38

标签: javascript php json ajax

我使用json_encode和ajax将php中的数组传递给Javascript。似乎唯一的方法是使用

echo json_encode($var)

还会因为回显而在页面上打印出$ var的信息。是否有一种隐藏输出的简单方法?我的代码如下,

main.php

<?php
include_once('testing.php');
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script type="text/javascript" src="testing.js"></script>
</body>
<html>

testing.php

<?php
$var=array('1','2','3','4','5');
echo json_encode($var);
?>      

testing.js

$.ajax({
        url : './testing.php',
        type : "GET",
        dataType : 'json',
        success : function (result) {
           showstaff(result);
        }
    });

function showstaff(x){
  console.log(x);
}

运行main.php打印出数组(&#39; 1&#39;,&#39; 2&#39;,&#39; 3&#39;,&#39; 4&#39;,&# 39; 5&#39;)在页面上显示控制台中的数组,但我需要的是在控制台中显示数组(即隐藏页面中的结果)。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

<script type="text/javascript">
    var object = <?php echo json_encode($var); ?>;
</script>

希望它有所帮助,谢谢!

答案 1 :(得分:0)

include_once('testing.php')中您不需要main.php。当main.php被发送到浏览器时,它将加载testing.js,然后通过AJAX调用执行testing.php

相关问题