在javascript中使用JSON.parse的Json_encode数据

时间:2015-08-31 06:07:04

标签: javascript php json

PHP文件

<?php
    include"connection.php";
    $id = $_GET['edit'];
    // echo $id;

    $sql = "SELECT * FROM form WHERE id=$id";
    $result = $conn->query($sql);

    $row=$result->fetch_assoc();
    $json_res=array();
    $json_res = array('user'=>($row));
    echo json_encode($json_res);

    $conn->close();
?>

JavaScript文件

<script type="text/javascript">
function loadup(str){

    var xmlhttp;
    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)
        {
            // var json = '{"user":[{"id":"45","name":"dfgMohan","loc":"Cherthala","dist":"Alappuzha","phone":"456","email":"fgdfgh@gmail.c","gender":"male","proofid":"2","sslc":"SSLC","plus2":"PLUS 2","degree":"DEGREE","pg":""}]}';

            var x=xmlhttp.responseText;                    

            var json1=JSON.parse(x);
            alert(json1.user[0].id);
            alert(json1.user[0].name);

            document.getElementById("myd").innerHTML=json1;
        }
    } 

    xmlhttp.open("GET","plain.php?edit="+str,true);
    xmlhttp.send();
}

</script>

为什么我不能提醒这个?

alert(json1.user[0].id);
alert(json1.user[0].name);

当我尝试提醒它时,没有显示警告信息。

当我警告某些消息时,它会在解析代码之前显示,然后在不执行之后显示。代码卡在解析代码行上。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<?php

include"connection.php";
$id = $_GET['edit'];
// echo $id;


$sql = "SELECT * FROM form WHERE id=$id";
$result = $conn->query($sql);

$row = $result->fetch_assoc();
$json_res = array();

array_push($json_res, $row);


echo json_encode($json_res);





$conn->close();
?>

javascript文件包含

<script type="text/javascript">
            function loadup(str) {

                var xmlhttp;
                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)
                    {
                        //var json = '{"user":[{"id":"45","name":"Raj Mohan","loc":"Cherthala","dist":"Alappuzha","phone":"9846320121","email":"rajmohan6535@gmail.c","gender":"male","proofid":"2","sslc":"SSLC","plus2":"PLUS 2","degree":"DEGREE","pg":""}]}';

                        var x = xmlhttp.responseText;


                       var json1 = JSON.parse(x);

                       document.getElementById("mydiv").innerHTML = "ID : "+json1[0].id;
                       document.getElementById("myd").innerHTML = "NAME : "+json1[0].name;



                    }
                }

                xmlhttp.open("GET", "plain.php?edit=" + str, true);
                xmlhttp.send();
            }

        </script>

当我们给予respomce时,请确保在php文件的页面中没有html包含...甚至注释行...他们将给出响应,我们无法解析响应文本所以要小心......当我们通过来自php dnt的响应添加或评论任何html包含在php(服务器点击页面)

我想你们都可以理解我的答案..如果不好请请原谅我..我的沟通非常低,而且是英文...

检查html是否包含在响应中你只是在javascript中提醒响应然后它显示任何html包含响应然后php有一些html包含那些我们无法解析它.....