js实现将数据写入到本地json文件问题


 

前提实例:

        // 渲染行政组织

        function setTree() {

                        $.ajax('http://10.131.111.91:8081/report/unitTree', {

                        dataType: 'json',

                                type: 'get',

                                timeout: 10000,

                                contentType: "application/x-www-form-urlencoded; charset=UTF-8",

                        success: function(data) {

                                        console.log("接口提交成功",data)

                        第一步:把返回的数据转成JSON格式

                                        var content = JSON.stringify(data);

第二步:把转成blob这种格式

                                        var blob = new Blob([content],

{type: "text/plain;charset=utf-8"});

第三步:调用保存saveAs 和文件名字

                                saveAs(blob, "root-org.json");

                               

                                       

                                },

                error: function(xhr, type, errorThrown) {

                                layer.msg("请求失败,请重试")

                                }

        //            });

 

  // }

 

第一步: 引用FileSaver.js

<script  src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.js"></script>

第二步: 在返回的json数据格式的组织中进行转换格式如下:

       

 

第一步:把返回的数据转成JSON格式

                var content = JSON.stringify(data);

第二步:把转成blob这种格式

var blob = new Blob([content], {type: "text/plain;charset=utf-8"});

第三步:调用保存saveAs 和文件名字

                saveAs(blob, "root-org.json");

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM