php文件如何查找以保存jquery ajax POST收到的文件? [重复]

问题描述 投票:0回答:1
我是一个菜鸟,正在为相对简单的东西而苦苦挣扎(或者我相信)。使用jquery ajax将json或更精确的geojson对象写入文件。到目前为止,我的方法看起来像这样

$.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 }'; ?>

    只是不要忘记在您的Apache中启用php ;-)再次感谢耐心的@ADyson带领初学者通过此过程。
php jquery ajax
1个回答
0
投票
尝试以下示例:

<?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" });

© www.soinside.com 2019 - 2024. All rights reserved.