Jquery Post方法在本地工作,但在服务器上不工作


Jquery Post Method works in local but not on the server

我有一个简单的函数,必须在文件上写入,它在我的本地服务器上工作,但当我在服务器上部署它时,它不在文件上写?怎么了?

foo.php

<?
if (isset($_POST['data'])) {
         $stringData = $_POST['data'];
         $file = "ciao.txt"; 
         $fh = fopen($file, 'w') or die("can't open file");
         fwrite($fh, $stringData);
         fclose($fh); 
 }
 ?>

函数.js

function WriteToFile() 
{
    var data = "foo baaar";
    $.post("JS/foo.php", {data: data}, function(result){ alert("label updated!!");}, "json");
}
<?
if (isset($_POST['data'])) {
         $stringData = $_POST['data'];
         $file = "ciao.txt"; 
         $fh = fopen($file, 'w') or die("can't open file");
         fwrite($fh, $stringData);
         fclose($fh); 
 }
 ?>

fwrite()返回写入的字节数,出错时返回FALSE。更改fwrite($fh,$stringData);

$write=fwrite($fh,$stringData);打印$writted;

现在你可以知道你的数据是否被写入。

或者您可以使用php代码设置文件权限chmod("/somedir/somefile",0755)//750或使用777进行测试,然后恢复到750或您的管理员建议的

/*试试这个*/

 if (isset($_POST['data'])) {
             $stringData = $_POST['data'];
             $file = "ciao.txt"; 
             chmod($file, 0755);
             $fh = fopen($file, 'w') or die("can't open file");
             fwrite($fh, $stringData);
             $written = fwrite($fh, $stringData);
             print $written;
             fclose($fh); 
     }

检查服务器上的文件权限。使其可写。