使用blob创建json文件

jos*_*ric 8 javascript jquery json blob

我在字符串中编写了json代码,我希望使用xmlhttp作为.json文件发送它.是否有可能用blob做到这一点?

var cleanScript = {
    'type': 'script',
    'api_key': api_key,
    'data': data,
    'inputs': inputs,
    'timeoutSeconds': timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript, null, 2); 
Run Code Online (Sandbox Code Playgroud)

现在json到blob?

Gil*_*mbo 24

尝试这样的事情

var cleanScript = {
    'type': 'script',
    'api_key': api_key,
    'data': data,
    'inputs': inputs,
    'timeoutSeconds': timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript);
var blob = new Blob([jsonse], {type: "application/json"});
var url  = URL.createObjectURL(blob);

var a = document.createElement('a');
a.href        = url;
a.download    = "backup.json";
a.textContent = "Download backup.json";

document.getElementById('json').appendChild(a);
Run Code Online (Sandbox Code Playgroud)
<div id="json"></div>
Run Code Online (Sandbox Code Playgroud)