$.ajax({
type: 'POST',
url: "data/markers.geojson", //url of receiver file on server
data: data, //geojson data
dataType: "json"
});
我认为不错,但很遗憾,markers.geojson文件仍然完全为空。如果我在“ $ .ajax”之前将数据变量的内容记录到控制台,则所有内容就在那里。网络控制台显示字节已传输。现在经过大量搜索后,我注意到其他所有人似乎都在将json数据发送到的不是.json文件,而是.php文件。不幸的是,他们似乎都在php方面做其他工作,或者没人认为有必要发布将接收到的json保存到服务器上文件的方法。
A,我是一个初学者,这里真的需要一些帮助-.php文件看起来应该如何将json保存到文件中?
编辑另一个问题中的这个答案解决了问题:
<?php $myFile = "testFile.txt"; $phpObj = json_decode($_POST['json']); file_put_contents($myFile,$phpObj); echo '{ "success": true }'; ?>
<?php
//error_reporting(E_ALL);
//var_dump($_SERVER);
$post_data = $_POST['data'];
if (!empty($post_data)) {
$dir = 'YOUR-SERVER-DIRECTORY/files';
$file = uniqid().getmypid();
$filename = $dir.$file.'.txt';
$handle = fopen($filename, "w");
fwrite($handle, $post_data);
fclose($handle);
echo $file;
}
?>
您的PHP文件桅杆在目录中具有名称:index.php:/ data / markers.geojson /
和您的代码:
$.ajax({ type: 'POST', url: "https://www.example.com/data/markers.geojson/", //url of receiver file on server data: data, //geojson data dataType: "json" });