我正在编写一个读取和写入包含HTML的JSON文件的jQuery应用程序。在我的本地Ubuntu测试服务器上执行下面显示的代码时,一切都按预期工作。在我的ISP在线服务器上执行相同的代码时,它的行为有所不同(并且失败)。这两个Web服务器都是Apache,并且在两种情况下都是从googleapis加载jQuery组件。
问题在于在JSON中保存的HTML中转义嵌入式双引号。当我在本地服务器上写文件时,转义会自动发生,如下所示:
<span class=\"heading\">
但是在将文件写入在线服务器后,它是这样的:
<span class="heading">.
由于每个JSON项本身都包含在&#34;看起来内部引号好像下次读取文件时会出错。
执行前的完整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文件是相同的。下面显示的应用程序中的所有缩减代码都是加载文件,显示内容并在按下&#34;保存您的工作后保存文件&#34;按钮。
在在线服务器上执行后的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>"]}
如果我在线运行,请按&#34;保存您的工作&#34;按钮,然后刷新页面,我看到我的console.log错误消息: JSON文件无效或未找到
我猜这是一个Web服务器设置,PHP设置或JSON版本(??),但不知道如何克服它。我尝试使用&#34;包含JSON项目,&#39;附上&#39;标题&#39;,&#39;处理&#39;等等。我试图扭转使用&#39;和&#34;,但是使用&#34;写出JSON;在所有情况下。
我应该补充说我有$ json = stripslashes($ json);在PHP中因为没有它我会得到多个\转义所有&#34;有三个反斜杠。
任何人都可以看到我做错的事吗?
本地服务器上的PHP是PHP Version 5.2.10-2ubuntu6 在线服务器上的PHP是PHP Version 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> </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);
?>
答案 0 :(得分:1)
你依靠magic_quotes_gpc开启:
$json = stripslashes($json);
成功:
if (get_magic_quotes_gpc()) $json = stripslashes($json);