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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀