douan8473 2017-04-26 01:09 采纳率: 100%
浏览 134

从服务器读取文本文件(Json)并发送到客户端(Javascript)

I am facing problem by using "JSON.stringify" and "Json.parse" for string. The application uses client broweser via Javascript to read a textfile (JSON string) from server via PHP.

The problem is that after I used "stringify", the output of the string printed as follow:

"


\t




{\"employees\":[{\"firstName\":\"John\",\"lastName\":\"Doe\" },{\"firstName\":\"Anna\",\"lastName\":\"Smith\" },{\"firstName\":\"Peter\",\"lastName\":\"Jones\" }]}

"
  • If not using "stringify", it printed as same as the original text in the text file.
  • If I use "JSON.stringify" then "Json.parse" it show error as below:

    Unexpected token < in JSON at position 0 at JSON.parse ()

The code are the following:

Sample of textfile:

{"employees":[{"firstName":"John","lastName":"Doe" },{"firstName":"Anna","lastName":"Smith" },{"firstName":"Peter","lastName":"Jones" }]}

HTML with Javascript page:

 <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>  

    <script>
    var text = '{"employees":[' +
    '{"firstName":"John","lastName":"Doe" },' +
    '{"firstName":"Anna","lastName":"Smith" },' +
    '{"firstName":"Peter","lastName":"Jones" }]}';

    function printEmployee() {  
        obj = JSON.parse(text);

        var selEmployee = document.getElementById("selEmployee");

        document.getElementById("demo").innerHTML =
        obj.employees[selEmployee.selectedIndex].firstName + " " + obj.employees[selEmployee.selectedIndex].lastName;   
    }

    function postToPhp() {
        var dataString = JSON.stringify(text);

        $.post("processJson.php",
            {
              dataString
            },
            function(data,status){
                alert("Data: " + data + "
Status: " + status);
            });
    }

    function postAsyncGet()
    {
        getToPhp()
            .then(viewGetResult);   
    }

    function getToPhp() {

        var dataString;

        $.get("readJson.php",
            {
              dataString
            },
            function(data,status){
                alert("Data: " + data + "
Status: " + status);
                viewGetResult(data);
            });
    }

    function viewGetResult(dataString) {

        var jsonstr = JSON.stringify(dataString.trim());
        //var obj = JSON.parse(dataString);
        document.getElementById("demo").innerHTML = dataString;

        var selEmployee = document.getElementById("selEmployee");

        //document.getElementById("demo").innerHTML =
        //obj.employees[selEmployee.selectedIndex].firstName + " " + //obj.employees[selEmployee.selectedIndex].lastName;   
    }

    </script>
    </head>

    <body>

    <h2>Create Object from JSON String</h2>

    <label>
    Select an Employee:
    </label>

    <select id = "selEmployee">
       <option value = "1" selected="selected">1</option>
       <option value = "2">2</option>
       <option value = "3">3</option>
    </select>

    <p><a href="#" onclick="printEmployee();">Print Me!</a></p>
    <p><a href="#" onclick="postToPhp();">Post Me to PHP!</a></p>
    <p><a href="#" onclick="getToPhp();">Get from PHP!</a></p>

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

    </body>
    </html>

PHP Code

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>  
</head>

<body>

<?php
$obj = json_decode($_POST["dataString"]);

echo $obj->var;

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $obj );
fclose($myfile);
?>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • duan1983 2017-04-26 10:46
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧