Simple save to JSON file with JQuery

前端 未结 1 965
你的背包
你的背包 2020-12-04 16:51

I\'ve tried out all examples I could get my hands on, yet I can\'t simply save JSON data to a JSON file on my host. I want to start with a simple as possible save method so

相关标签:
1条回答
  • 2020-12-04 17:44

    $.ajax alone will not save the json file, you need to direct the url property to a server-side script, i.e. http://your.host/save_json.php, that will create general.json and write your output on it. Something like:

    PHP:

    <?php
    $myFile = "general.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $_GET["data"];
    fwrite($fh, $stringData);
    fclose($fh)
    ?>
    

    You'll also need to change the data property in your ajax call to data: {data: JSON.stringify(eventsholded)} to give the GET variable a proper name that can be retrieved from PHP:

    JQUERY

    $.ajax
        ({
            type: "GET",
            dataType : 'json',
            async: false,
            url: 'http://your.host/save_json.php',
            data: { data: JSON.stringify(eventsholded) },
            success: function () {alert("Thanks!"); },
            failure: function() {alert("Error!");}
        });
    
    0 讨论(0)
提交回复
热议问题