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

时间:2017-04-26 01:09:45

标签: javascript php jquery json

我通过使用“JSON.stringify”和“Json.parse”来表示问题。该应用程序通过Javascript使用客户端浏览器通过PHP从服务器读取文本文件(JSON字符串)。

问题是,在我使用“stringify”之后,字符串的输出打印如下:

"\r\n\r\n\r\n\t\r\n\r\n\r\n\r\n\r\n{\"employees\":[{\"firstName\":\"John\",\"lastName\":\"Doe\" },{\"firstName\":\"Anna\",\"lastName\":\"Smith\" },{\"firstName\":\"Peter\",\"lastName\":\"Jones\" }]}\r\n\r\n"
  • 如果不使用“stringify”,则打印方式与原始文本相同 文本文件。
  • 如果我使用“JSON.stringify”,那么“Json.parse”会显示如下错误:

    意外的令牌<在位置0的JSON中     在JSON.parse()

代码如下:

文本文件示例:

{“employees”:[{“firstName”:“John”,“lastName”:“Doe”},{“firstName”:“Anna”,“lastName”:“Smith”},{“firstName”: “彼得”,“姓氏”:“琼斯”}}}

带有Javascript页面的HTML:

 <!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 + "\nStatus: " + status);
            });
    }

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

    function getToPhp() {

        var dataString;

        $.get("readJson.php",
            {
              dataString
            },
            function(data,status){
                alert("Data: " + data + "\nStatus: " + 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代码

<!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 个答案:

答案 0 :(得分:0)