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:
我只想在服务器上的文件中保存一些JSON(使用Javascript生成)。但我甚至不能使用简单的字符串:
HTML File:
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>
And the php file is something like this:
而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);
Nothing worked. I also tried
没有任何效果。我也试过了
$.ajax({
var datatosend="foo bar";
url: "page.php",
data: datatosend,
processData: false
});
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.
我不知道什么是错的。单击html文件中的div后,txt文件就在那里。但文件中没有内容。如果我只是将$ _POST写入文本文件,该文件包含Text“Array”,这意味着$ _POST有一些内容。
4 个解决方案
#1
2
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调用正在使用POST方法。
$.ajax({
url: "page.php",
type : 'POST',
...
});
As sated in the jQuery documentation, the type parameter sets the request type, POST or GET.
正如jQuery文档中所述,type参数设置请求类型POST或GET。
The type of request to make ("POST" or "GET"), default is "GET".
要求的类型(“POST”或“GET”),默认为“GET”。
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 -
另一件需要考虑的事情是你实际上并没有编写JSON数据。数据以该格式发送,但当它到达$ _POST变量时,它已被转换为数组。你应该尝试做的是将PHP数组写入文件 -
$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'
)
?>
As you can see, the var_export()
function returns a parsable version of the variable.
如您所见,var_export()函数返回变量的可解析版本。
var_export()
- var_export()
var_export — Outputs or returns a parsable string representation of a variable
var_export - 输出或返回变量的可解析字符串表示形式
#2
0
You need to make a POST
call, see below:
您需要进行POST呼叫,请参阅以下内容:
$('#test').click(function(){
$.ajax({
url: "page.php",
type : 'post',
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
});
#3
0
By default, all $.ajax() requests are sent using the GET method, not POST. You'll need to specify the type as 'POST':
默认情况下,所有$ .ajax()请求都是使用GET方法发送的,而不是POST。您需要将类型指定为“POST”:
$.ajax({
var datatosend="foo bar";
url: "page.php",
type: 'post',
data: datatosend,
processData: false
});
From the documentation:
从文档:
type - String
type - String
Default: 'GET'
默认值:'GET'
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
要求的类型(“POST”或“GET”),默认为“GET”。注意:此处也可以使用其他HTTP请求方法,例如PUT和DELETE,但并非所有浏览器都支持它们。
#4
0
May be it should be
可能是应该的
$.ajax({
url: "page.php",
type: "POST", // add this
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
OR
要么
in PHP use $_REQUEST
instead of $_POST
在PHP中使用$ _REQUEST而不是$ _POST
#1
2
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调用正在使用POST方法。
$.ajax({
url: "page.php",
type : 'POST',
...
});
As sated in the jQuery documentation, the type parameter sets the request type, POST or GET.
正如jQuery文档中所述,type参数设置请求类型POST或GET。
The type of request to make ("POST" or "GET"), default is "GET".
要求的类型(“POST”或“GET”),默认为“GET”。
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 -
另一件需要考虑的事情是你实际上并没有编写JSON数据。数据以该格式发送,但当它到达$ _POST变量时,它已被转换为数组。你应该尝试做的是将PHP数组写入文件 -
$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'
)
?>
As you can see, the var_export()
function returns a parsable version of the variable.
如您所见,var_export()函数返回变量的可解析版本。
var_export()
- var_export()
var_export — Outputs or returns a parsable string representation of a variable
var_export - 输出或返回变量的可解析字符串表示形式
#2
0
You need to make a POST
call, see below:
您需要进行POST呼叫,请参阅以下内容:
$('#test').click(function(){
$.ajax({
url: "page.php",
type : 'post',
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
});
#3
0
By default, all $.ajax() requests are sent using the GET method, not POST. You'll need to specify the type as 'POST':
默认情况下,所有$ .ajax()请求都是使用GET方法发送的,而不是POST。您需要将类型指定为“POST”:
$.ajax({
var datatosend="foo bar";
url: "page.php",
type: 'post',
data: datatosend,
processData: false
});
From the documentation:
从文档:
type - String
type - String
Default: 'GET'
默认值:'GET'
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
要求的类型(“POST”或“GET”),默认为“GET”。注意:此处也可以使用其他HTTP请求方法,例如PUT和DELETE,但并非所有浏览器都支持它们。
#4
0
May be it should be
可能是应该的
$.ajax({
url: "page.php",
type: "POST", // add this
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
OR
要么
in PHP use $_REQUEST
instead of $_POST
在PHP中使用$ _REQUEST而不是$ _POST