我正在考虑将 json 对象发布到 PHP 页面的网页,然后 PHP 页面生成一些数据并将数据导出到本地文件。因为在第一步中,数据会太大而无法通过URL,我需要使用JSON。但是,当我以 AJAX 方式将 JSON 对象传递给 PHP 时,不会触发下载。我想知道如何触发此下载。
//the code will not trigger a download from PHP
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST", "export.php");
xmlhttp.setRequestHeader("Content-type", "application/json", true);
xmlhttp.send(JSON.stringify(obj));
我可以在 JavaScript 中接收回显数据,但在 JavaScript 中创建本地文件比在 PHP 中更复杂。