且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP脚本保存JSON将文件保留为空白

更新时间:2023-11-30 23:20:10

在您的php文件中尝试

Try this in your php file

<?php
$myFile = "json/countries.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = urldecode($_POST["data"]);
$stringData1 = json_encode(json_decode($stringData));
fwrite($fh, $stringData1);
fclose($fh)
?>

将此添加到您的js文件中

Add this in your js file

saveJson(countries, '/save_countries_to_json.php');

function saveJson(object, file) {

$.ajax
({
    type: "POST",
    dataType : 'json',
    async: false,
    url: file,
    data: { data:object },
    success: function () {alert("Thanks!"); },
    failure: function() {alert("Error!");}
});
}