上述问题有时才会发生。我不知道为什么,但我假设我的php脚本(用于在JSON文件中保存和加载JSON对象)并没有完美地完成。
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;在这里,我的javascript代码:
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);
}
})
})
};我查了一下分段故障的定义,但没有真正得到“aaaaah.当然,这就是原因”。
发布于 2019-03-25 22:27:11
尝试删除fopen、fwrite和fclose。在第一种情况下,您只需要file_put_contents(),在第二只需要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;https://stackoverflow.com/questions/55347252
复制相似问题