我只想在服务器上的文件中保存一些 JSON(使用 Javascript 生成)。但我什至不让它与一个简单的字符串一起工作:
HTML 文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<style>
#test{
padding:20px 50px;
background:#ccc;
color:#000;
}
</style>
<script>
$(function(){
$('#test').click(function(){
$.ajax({
url: "page.php",
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
});
});
</script>
</head>
<body>
<div id="test">
KLICK
</div>
</body>
</html>
php文件是这样的:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
fwrite($fh,$_POST['data']);
fwrite($fh,$_POST['foo']);
fwrite($fh,$_POST["foo"]);
fwrite($fh,$_POST[foo]);
fclose($fh);
没有任何效果。我也试过
$.ajax({
var datatosend="foo bar";
url: "page.php",
data: datatosend,
processData: false
});
我不知道有什么问题。单击html文件中的div后,txt文件就在那里。但是文件中没有内容。如果我只是将 $_POST 写入文本文件,则该文件包含文本“数组”,这意味着 $_POST 有一些内容。