Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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
如何使用json和ajax从PHP数组创建javascript数组?_Javascript_Php_Arrays_Ajax_Json - Fatal编程技术网

如何使用json和ajax从PHP数组创建javascript数组?

如何使用json和ajax从PHP数组创建javascript数组?,javascript,php,arrays,ajax,json,Javascript,Php,Arrays,Ajax,Json,尝试使用JSON和AJAX从URL的PHP数组到图像创建javaScript数组。然后我想在cordova应用程序上显示图像。我相信我的问题是,我不是在接收或发送带有自动html标记的Json消息。我的xmlhttp.response与URL数组相等,但它具有PHP文件中不存在的html标记。我想因为这些标签,我的JSON.parse不能工作了 这是我的PHP存储在服务器上。我在json消息中返回URL <?php include("mysqlconnect.php"); $select

尝试使用JSON和AJAX从URL的PHP数组到图像创建javaScript数组。然后我想在cordova应用程序上显示图像。我相信我的问题是,我不是在接收或发送带有自动html标记的Json消息。我的xmlhttp.response与URL数组相等,但它具有PHP文件中不存在的html标记。我想因为这些标签,我的JSON.parse不能工作了

这是我的PHP存储在服务器上。我在json消息中返回URL

<?php
include("mysqlconnect.php");

$select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error());   


$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $row['ImagesPath'];


}

echo json_encode($data);

?>

这是我在cordova应用程序中的脚本。我想将JSON消息放入一个javaScript数组,并在div内容中显示数组中的第一个图像。但是我想我调用的是一条包含html标记的消息,因此我无法解析该消息,我可能是错的。我目前收到三个错误警报,因为就绪状态不正确

 <html>
 <script>

 var arr = [];

      function importJson(str) {

            if (str=="") {
                document.getElementById("content").innerHTML="";
                return;
            }
            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){

                alert(xmlhttp.response);

                arr = JSON.parse(xmlhttp.response);
                //var arr = (array)json.parse(xmlhttp.response);
                for (var i = 0; i < arr.length; i++) {
                    buildImage(arr[i]);
                    alert(arr[0]);
                }

            } else {


            alert("Error");
            }


            }



            xmlhttp.open("GET","http://server/~name/folder/content.php);
            xmlhttp.responseType = "json";
            xmlhttp.send();

            buildImage=function(src) {
                var img = document.createElement('img');
                img.src = src;
                document.getElementById("content").appendChild(img);

            }
      }

    window.onload = importJson();

 </script>


<body>
<div id="content"></div>

var-arr=[];
函数importJson(str){
如果(str==“”){
document.getElementById(“内容”).innerHTML=“”;
回来
}
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
警报(xmlhttp.response);
arr=JSON.parse(xmlhttp.response);
//var arr=(array)json.parse(xmlhttp.response);
对于(变量i=0;i
目前,您的PHP脚本正在输出网页,但javascript正在寻找JSON。要输出JSON,您需要在PHP脚本中设置标题:

header('Content-Type: application/json');
echo json_encode($data);

您是否检查了php脚本输出的json、脚本接收的内容以及json解析的每个阶段都发生了什么?您的JS中也有一些输入错误。php应该只输出数组,但它接收数组和html标记。在哪里?echo json_encode($data)的输出是什么?[”http:\/\/server\/~name\/folder\/images\/22-08-2014-1408726981.jpg“,”http:\/\/server\/~name\/folder\/images\/07-08-2014-1407418088.png“]类似这样;;[“http:\/\/server\/~name\/folder\/images\/22-08-2014-1408726981.jpg“,”htt‌​p:\/\/server\/~name\/folder\/images\/07-08-2014-1407418088.png“]我一直收到的响应是邮件头已经发送了,我无法修改它们?我在我的“include”(“mysqlconnect.php”)中找到了html标记;“这就是问题所在。谢谢你,酷。最好在脚本开头设置标题以避免出现问题。此外,mysql_*命令正在被弃用——如果可能的话,你应该使用mysqli_*或PDO。请参阅。”