相同的jQuery代码在本地和在线服务器上编写不同的JSON数据


Same jQuery code writes different JSON data on local and online server

我正在编写一个jQuery应用程序,读取和写入包含HTML的JSON文件。当在我本地的Ubuntu测试服务器上执行下面的代码时,一切都如预期的那样工作。当在我的isp在线服务器上执行相同的代码时,它的行为不同(并且失败)。两个web服务器都是Apache, jQuery组件都是从googleapi加载的。

问题是在JSON中保存的HTML中转义嵌入双引号。当我在本地服务器上写入文件时,会自动进行转义,如下所示:

<span class='"heading'"> 

但是在在线服务器上写入文件后,它是这样的:

<span class="heading">. 

由于每个JSON项本身都包含在"中,因此看起来好像内部引号会在下次读取文件时导致错误。

执行前的完整JSON文件:

{"userItem":["<li><span class='"heading'"><span class='"handle'"><span class='"ui-icon ui-icon-arrowthick-2-n-s'"></span></span>Heading 1</span></li>","<li><span class='"item'"><span class='"handle'"><span class='"ui-icon ui-icon-arrowthick-2-n-s'"></span></span>Action 1</span></li>"]}

本地服务器上执行后写入的JSON文件是相同的。下面所示的应用程序的所有代码都是加载文件,显示内容并在按下"保存工作"按钮后保存文件。

online服务器上执行后的JSON数据如下所示:

{"userItem":["<li><span class="heading"><span class="handle"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></span>Heading 1</span></li>","<li><span class="item"><span class="handle"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></span>Action 1</span></li>"]}

如果我在线运行这个,按下"Save your work"按钮,然后刷新页面,我看到我的console.log错误信息:JSON文件无效或未找到

我猜这是一个web服务器设置,PHP设置或JSON版本(?? ?),但不知道如何克服它。我尝试使用"包含JSON项目"answers"包含"标题","句柄"等。我试着反转' And '的使用,但JSON在所有情况下都使用' '。

我应该补充说,我有$json = stripslashes($json);因为如果没有它,我会得到多个'转义的所有"与三个反斜杠。

有谁能看出我做错了什么吗?

本地服务器的PHP版本为PHP Version 5.2.10-2ubuntu6在线服务器的PHP版本为PHP 5.3.28

如果这是一个PHP版本问题,有什么建议克服它吗?

<!DOCTYPE html>
<html>
     <head>
        <link type="text/css" rel="stylesheet" href="todo.css"/>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script>
// Function to  READ  existing data from JSON file
            // Assign handlers immediately after making the request
            // and load data to HTML
            function loadData() {
                $.getJSON('todo.json',function(data){
                    console.log('success');
                    $.each(data.userItem,function(i,item){
                        $('.list').append(item);
                    });
                }).error(function(){
                    console.log('JSON file invalid or not found');
                });
            }
// Function to  WRITE  data to JSON file
            function writeData() {
                var jsonObject = { "userItem" : [] };
                var allUserItems = $("li"); 
                    for ( var index = 0; index < allUserItems.length; index++) {
                    jsonObject.userItem[index] = allUserItems[index].outerHTML;
                };
                // some jQuery to write to file
                $.ajax( {
                    type : "POST",
                    url : "todojson.php",
                    data : { json : JSON.stringify(jsonObject) },
                    success: function () {console.log("Good!"); },
                    error: function() {console.log("Error!");}
                });
            }
            $(document).ready(function () {
                loadData (); // get and display JSON data from past sessions
                // SAVE contents of page to json file when 'Save your work' button pressed
                $('#buttonSave').click(function () { // code for the blue Save button
                    $( this ).animate({ color: "#00ff00" }, 300 );
                    writeData();
                    $( this ).animate({ color: "#fff" }, 500 );
                });
            });
        </script>
        <title>Action list</title>
    </head>
    <body>
        <div id="controls" style="width: 100%">
            <div id="buttonSave"> Save your work </div>
        </div>
        <p>&nbsp;</p>
        <ul class="list">
            <!-- HTML from JSON file loaded here -->
        </ul>
    </body>
</html>

写JSON的PHP是:

<?php
$json = $_POST['json'];
$json = stripslashes($json);
$file = fopen('todo.json','w+');
fwrite($file, $json);
fclose($file);
?>

您依赖magic_quotes_gpc打开:

$json = stripslashes($json);

让它:

if (get_magic_quotes_gpc()) $json = stripslashes($json);