0
我正在尝试构建一个Web应用程序,用户可以使用OpenLayers绘制功能,然后将它们保存为下载或写入服务器上的文件,我可以在需要时收集这些文件。到目前为止,我对OL3位感到满意。从阅读周围看来,AJAX是这样做的,所以我设法在我的HMTL中包含一个按钮,该按钮运行一个运行php文件的函数来创建一个新文件。我很高兴我设法得到一个空文本文件作为开始。那么如何让该文件包含JSON信息?我想我需要将它从JS世界发送到PHP以某种方式?我看到一个使用jquery的答案,它通过发布后发送数据,但我无法弄清楚如何在我的页面上运行该脚本。使用AJAX保存JSON数据
的功能
function loadXMLDoc()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","run.php",true);
xmlhttp.send();
}
</script>
按钮
<button type="button" onclick="loadXMLDoc()">do something</button>
The PHP file (run.php)
<?php
$myfile = fopen("store/anothernewfile.geojson", "w") or die("Unable to open file!");
$txt = ['lyr_site'];
fwrite($myfile, $txt);
fclose($myfile);
?>
你的代码的工作,我几乎没有。但GEOJson文件的内容具有额外的斜杠加载函数loadXMLDoc()var geoJSON = new ol.format.GeoJSON(); var jsondata = geoJSON.writeFeatures(lyr_site.getSource()。getFeatures()); var xmlhttp = new XMLHttpRequest(); xmlhttp.open('POST','run.php',true); xmlhttp.setRequestHeader('Content-Type','application/json; charset = UTF-8'); xmlhttp.send(JSON.stringify(jsondata));' – Sethinacan
额外的斜线似乎是因为'.stringify',所以我删除了它,并且我得到一个打开并具有属性但没有空间范围的文件,即使该文件充满了坐标。 'xmlhttp.send(jsondata);' – Sethinacan
该文件基本没问题,但缺少以下这行,如果我手动添加它可以正常工作。 '“crs”:{“type”:“name”,“properties”:{“name”:“urn:ogc:def:crs:EPSG :: 3857”}},' – Sethinacan