使用json将php数组传递给javascript

使用json将php数组传递给javascript,javascript,php,jquery,html,json,Javascript,Php,Jquery,Html,Json,这是php脚本: <?php //this line is necessary to make JS and php communicate with json //header("Content-type: text/javascript"); //$searchQuery = "facebook"; $searchQuery = $_GET['q']; $googleUrl='http://www.google.com/search?q='.$

这是php脚本:

<?php
    //this line is necessary to make JS and php communicate with json
    //header("Content-type: text/javascript");
    //$searchQuery = "facebook";

    $searchQuery = $_GET['q'];
    $googleUrl='http://www.google.com/search?q='.$searchQuery;
    $googleSource = @file_get_contents($googleUrl);
    $bingUrl = 'http://www.bing.com/search?q=' .$searchQuery.'&first=1';
    $bingSource = @file_get_contents($bingUrl);
    $bingGoogleArray = array($bingSource,$googleSource);
    echo json_encode($bingGoogleArray);
    //echo $googleSource;
?>
这是html页面:

<html>
    <head>
        <title>Test Ajax And Jquery</title>
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
        <script>
        function ajaxTest() {
            //var string = "<html><body><b>Hello World</b></body></html>";
            //$('#test').html(string);
            $.ajax({url:"testJqueryAjax.php?q=facebook" , success: function(result){
                $('h1').empty();
                //var data = JSON.parse(result);
                var data = $.parseJSON(result);
                $('p').html(data[1]);   
                //alert(data[1]);
            }});
        }
        </script>
    </head>
    <body>
        <div id="search" style="height:15%;">
            <button onClick="ajaxTest()">Submit</button>
        <h1>Loading</h1>
    </div>


    <p></p>

    </body>
</html>
这个小型web应用程序的作用是,php脚本在google和bing上搜索关键字,并返回一个json编码的数组,该数组包含bing搜索页面和google页面的html源代码

在html页面上,我解析从php脚本接收到的json_编码数组,并将数据[i]复制到html标记中

问题是,当我将包含Bing搜索页面的数据[0]复制到html标记时,结果会出现,但当我将包含Google搜索页面的数据[1]复制到html标记时,不会出现任何结果!:


您可能会说$googleSource是空的,但是当我单独从php脚本发送$googleSource时,它就工作了

我检查了你的来源,对我来说,$googleSource是空的。错误是:json_encode:参数中的UTF-8序列无效

所以试着用这句话:

$googleSource = utf8_encode(@file_get_contents($googleUrl));

像这样的问题一天要问10次。只需在主页上看几分钟!是否在服务器上完成了任何基本调试,如var_dump$bingGoogleArray?另外,请注意,这样的搜索结果是对谷歌ToS的违反。似乎你不理解我,问题是当我单独发送$googleSource时,它可以以html显示。但如果我在json编码的数组中发送$googleSource和$bingSource,则只能显示$bingSource。@我搜索了TomášZato,但我问题的答案不存在。