The above mentioned problem occurs only sometimes. I have no idea why but I assume that my php script for saving and loadinf a JSON object in a JSON-file is not perfectly done.
writeLanguage.php
<?php $myFile = "languages.json"; $cmsData = $_POST['data']; $obj = json_decode($cmsData, true); $myFile = "language.json"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh,json_encode($obj)); fclose($fh);
readLanguage.php
$cmsData = $_GET['data']; $myFile = "language.json"; $fhh = fopen($myFile, 'r') or die("can't open file"); $jsondata = file_get_contents($myFile); fclose($fhh); $json = json_decode($jsondata, true); echo $jsondata;
here my javascript code:
DataService.prototype.loadLanguagePromise = function () { return new Promise(function (resolve, reject) { $.ajax({ url: "php/services/readLanguage.php", type: "GET", async: true, dataTyp: 'json', success: function (data) { resolve("stuff worked"); }, error: function (xhr, desc, err) { reject(Error("It broke")); } }) }) }; DataService.prototype.saveLanguage = function (cmsObject) { return new Promise(function (resolve, reject) { $.ajax({ url: "php/services/writeLanguage.php", type: "POST", data: {data: JSON.stringify(cmsObject)}, dataTyp: 'json', success: function (data) { resolve(data); }, error: function (xhr, desc, err) { reject(xhr, desc, err); } }) }) };
I looked up for the definiton of Segmentation fault but could not really get an “aaaaah… of course, that’s why”.
Advertisement
Answer
Try to delete fopen, fwrite and fclose. In first case you need only file_put_contents(), in second – ONLY file_get_contents.
<?php $myFile = "languages.json"; $cmsData = $_POST['data']; $obj = json_decode($cmsData, true); $fh = file_put_contants($myFile, $cmsData,LOCK_EX) or die("can't open file");
$cmsData = $_GET['data']; $myFile = "language.json"; $jsondata = file_get_contents($myFile); $json = json_decode($jsondata, true); echo $jsondata;