Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从服务器上的PHP文件中获取JSON格式的数据_Javascript_Php_Json_Phpmyadmin_Xmlhttprequest - Fatal编程技术网

Javascript 无法从服务器上的PHP文件中获取JSON格式的数据

Javascript 无法从服务器上的PHP文件中获取JSON格式的数据,javascript,php,json,phpmyadmin,xmlhttprequest,Javascript,Php,Json,Phpmyadmin,Xmlhttprequest,我在服务器上有一个数据库,其中包含一个表customers,以及列names。我想向服务器发出请求,在那里我请求customers表中的前2条记录。一旦我运行程序,浏览器就无法显示记录,它显示undefined。请看下图 .php: .html: <p id="demo"></p> <script> var obj, dbParam, xmlhttp, myObj, x, txt = ""; obj = { "table":"customers

我在服务器上有一个数据库,其中包含一个表
customers
,以及列
names
。我想向服务器发出请求,在那里我请求
customers
表中的前2条记录。一旦我运行程序,浏览器就无法显示记录,它显示undefined。请看下图

.php:


.html:

    <p id="demo"></p>

<script>
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "table":"customers", "limit":2 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        for (x in myObj) {
            txt += myObj[x].name + "<br>";
        }
        document.getElementById("demo").innerHTML = txt;
    }
};
xmlhttp.open("GET", "demo_file.php?x=" + dbParam, true);
xmlhttp.send();

</script>

var obj,dbParam,xmlhttp,myObj,x,txt=“”; obj={“表”:“客户”,“限额”:2}; dbParam=JSON.stringify(obj); xmlhttp=新的XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ myObj=JSON.parse(this.responseText); 对于(myObj中的x){ txt+=myObj[x]。名称+“
”; } document.getElementById(“demo”).innerHTML=txt; } }; open(“GET”、“demo_file.php?x=“+dbParam,true”); xmlhttp.send();
您必须使用js中循环对象的
名称
而不是
名称
,因为在select查询中,您有
名称
列,结果有
名称
属性

<p id="demo"></p>

<script>
    var obj, dbParam, xmlhttp, myObj, x, txt = "";
    obj = { "table":"customers", "limit":2 };
    dbParam = JSON.stringify(obj);
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            myObj = JSON.parse(this.responseText);
            console.log(myObj)
            for (x in myObj) {
                txt += myObj[x].names + "<br>";
            }
            document.getElementById("demo").innerHTML = txt;
        }
    };
    xmlhttp.open("GET", "demo_file.php?x=" + dbParam, true);
    xmlhttp.send();

</script>

var obj,dbParam,xmlhttp,myObj,x,txt=“”; obj={“表”:“客户”,“限额”:2}; dbParam=JSON.stringify(obj); xmlhttp=新的XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ myObj=JSON.parse(this.responseText); console.log(myObj) 对于(myObj中的x){ txt+=myObj[x]。名称+“
”; } document.getElementById(“demo”).innerHTML=txt; } }; open(“GET”、“demo_file.php?x=“+dbParam,true”); xmlhttp.send();
查看浏览器的开发人员控制台,了解Ajax请求的响应详细信息。Hamedani,你太棒了!简单的错误,我无法察觉。谢谢,现在可以用了。
<p id="demo"></p>

<script>
    var obj, dbParam, xmlhttp, myObj, x, txt = "";
    obj = { "table":"customers", "limit":2 };
    dbParam = JSON.stringify(obj);
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            myObj = JSON.parse(this.responseText);
            console.log(myObj)
            for (x in myObj) {
                txt += myObj[x].names + "<br>";
            }
            document.getElementById("demo").innerHTML = txt;
        }
    };
    xmlhttp.open("GET", "demo_file.php?x=" + dbParam, true);
    xmlhttp.send();

</script>