保存数据通过Ajax POST和PHP到文件 [英] Save Data to File via Ajax POST and PHP

查看:134
本文介绍了保存数据通过Ajax POST和PHP到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想省点JSON(与JavaScript生成)在服务器上的文件。 不过,我甚至不明白一个简单的字符串工作:

I just want to save some JSON (generated with Javascript) in a file on the server. But I even don't get it to work with a simple string:

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文件是这样的:

And the php file is something like this:

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

毫无效果。我也试过

Nothing worked. I also tried

$.ajax({
    var datatosend="foo bar";
    url: "page.php",
    data: datatosend,
    processData: false
    });

我不知道什么可能是错误的。 TXT文件是否有点击的HTML文件的股利后。但是,文件中没有的内容。如果我只是写$ _ POST到文本文件,该文件包含文本阵列,意思是$ _ POST有一些内容。

I don't know what could be wrong. The txt file is there after clicking the div in the html file. But there is no content in the file. If I just write $_POST to the text file, the file contains the Text "Array", meaning that $_POST has some content.

推荐答案

几件事情可能是错的怎么回事。请检查您正在试图写该目录的权限。另外,还要确保你的Ajax调用使用POST方法。

Few things could be going wrong here. Check the permissions on the directory your are trying to write to. Also make sure that your ajax call is using the POST method.

$.ajax({
  url: "page.php",
  type : 'POST',
  ...
}); 

由于心满意足的 jQuery的文档,类型参数设置请求类型,POST或GET。

As sated in the jQuery documentation, the type parameter sets the request type, POST or GET.

请求的类型,使(POST或GET),默认是GET。

The type of request to make ("POST" or "GET"), default is "GET".

另一个要考虑的是,你实际上并没有写JSON数据。该数据被发送的格式,但是当它到达 $ _ POST 变量已经被改造成一个阵列。你应该尝试做的是写一个PHP数组到文件 -


Another thing to consider is that you are not actually writing JSON data. The data is send in that format but when it gets to the $_POST variable it has been converted into an array. What you should try doing is writing a PHP array to the file -

$fh = fopen($myFile, 'w');
fwrite($fh,'<?php $arr='.var_export($_POST['data'],true).' ?>');
fclose($fh);

这应该给类似这样的一个文件 -

That should give a file similar to this -

<?php
  $arr = array(
    'foo'=>'bar'
  )
?>

正如你所看到的, var_export()函数返回变量的解析的版本。

As you can see, the var_export() function returns a parsable version of the variable.

var_export - 输出或返回一个可分析的字符串再一个变量presentation

var_export — Outputs or returns a parsable string representation of a variable

这篇关于保存数据通过Ajax POST和PHP到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆