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 overleaf中论文编辑,报错`pages' is a missing field, not a string, for entry 4
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么