douhuan6157 2014-12-25 13:50
浏览 62
已采纳

如何打印xmlhttp响应?

I am getting some information from database, this information is getting back into JSON format now I need to print this JSON information. But my code is not working getCountryDetails.php is php file for interacting with the database. Following code is the script, When I click the button It intersects with database.

<script type="text/javascript">
    $(document).ready(function() {
        $("#quickSearch").click(function(){       
            var countries = [];
            $.each($("#select-choice-1 option:selected"), function(){ 
                countries.push($(this).val());
            });

    if (countries == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {    
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    //myFunction(xmlhttp.responseText);


                    myFunction(xmlhttp.responseText);
                }
            }
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            xmlhttp.open("GET","webservices/getCountryDetails.php?q="+countries,true);
            xmlhttp.send();
        } 
        });
    });
    function myFunction(response) {
        var arr = JSON.parse(response);
        var i;
        var out = "<table>";

        for(i = 0; i < arr.length; i++) {
            out += "<tr><td>" +
            arr[i].Name +
            "</td><td>" +
            arr[i].City +
            "</td><td>" +
            arr[i].Country +
            "</td></tr>";
        }
        out += "</table>"
        document.getElementById("id01").innerHTML = out;
}
</script>
  • 写回答

1条回答 默认 最新

  • duanlingzei0170 2014-12-25 13:58
    关注

    Firstly, countries will never be an empty string, you've defined it as an array

    var countries = [];
    
    ...
    
    if (countries == "") { // always fail
    

    secondly, you can't concantenate an array into a string, and XMLHttpRequest doesn't accept arrays

    xmlhttp.open("GET","webservices/getCountryDetails.php?q=" + countries, true);
    

    Thirdly, you seem to be using jQuery, so why not use it as it does accept an array

    $.ajax({
        url  : 'webservices/getCountryDetails.php',
        data : countries
    }).done(myFunction);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示