javascript - Same jQuery code writes different JSON data on local and online server -
i'm writing jquery app reads , writes json file containing html. when executing code shown below on local ubuntu test server works expected. when executing identical code on isps on-line server behaves differently (and fails). both web servers apache , jquery components loaded googleapis in both cases.
the problem escaping embedded double-quotes in html saved in json. escaping happens automatically when write file on local server, this:
<span class=\"heading\"> but after writing file on online server, it's this:
<span class="heading">. since each json item enclosed in " looks if inner quotes cause error next time file read.
full json file before execution:
{"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>"]} the json file written after execution on local server identical. cutting downwards code app shown below load file, display contents , save file after pressing "save work" button.
the json info after execution on online server this:
{"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>"]} if run online, press "save work" button, refresh page, see console.log error message: json file invalid or not found
i'm guessing web server setting, php setting or json version (??) have no thought how overcome it. tried using " enclose json items, , ' enclose 'heading', 'handle', etc. , tried reversing utilize of ' , ", json written out using " in cases.
i should add together have $json = stripslashes($json); in php because without multiple \ escaping of " 3 backslashes.
can see i'm doing wrong, please?
php on local server php version 5.2.10-2ubuntu6 php on online server php version 5.3.28
if it's php version issue, suggestions overcoming it?
<!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 read existing info json file // assign handlers after making request // , load info 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 write info json file function writedata() { var jsonobject = { "useritem" : [] }; var alluseritems = $("li"); ( var index = 0; index < alluseritems.length; index++) { jsonobject.useritem[index] = alluseritems[index].outerhtml; }; // jquery write file $.ajax( { type : "post", url : "todojson.php", info : { json : json.stringify(jsonobject) }, success: function () {console.log("good!"); }, error: function() {console.log("error!");} }); } $(document).ready(function () { loaddata (); // , display json info past sessions // save contents of page json file when 'save work' button pressed $('#buttonsave').click(function () { // code bluish save button $( ).animate({ color: "#00ff00" }, 300 ); writedata(); $( ).animate({ color: "#fff" }, 500 ); }); }); </script> <title>action list</title> </head> <body> <div id="controls" style="width: 100%"> <div id="buttonsave"> save work </div> </div> <p> </p> <ul class="list"> <!-- html json file loaded here --> </ul> </body> </html> the php writes json is:
<?php $json = $_post['json']; $json = stripslashes($json); $file = fopen('todo.json','w+'); fwrite($file, $json); fclose($file); ?>
you rely on magic_quotes_gpc beingness on:
$json = stripslashes($json); make it:
if (get_magic_quotes_gpc()) $json = stripslashes($json); javascript php jquery ajax json
No comments:
Post a Comment