$.ajax将数组作为Json发布到PHP$_POST=空
好吧,我花了很多时间在这上面,我不明白我做错了什么 似乎无法获取PHP文件中的数据$.ajax将数组作为Json发布到PHP$_POST=空,php,javascript,jquery,ajax,json,Php,Javascript,Jquery,Ajax,Json,好吧,我花了很多时间在这上面,我不明白我做错了什么 似乎无法获取PHP文件中的数据 首先,我多次调用“copy”来填充“result”数组 然后,我调用$.ajax方法 在process.php$\中,POST为空 -->在PHP中,$x、$y或$time或不为null但不为空 编辑2: <script src="jquery.js"></script> results = new Array(); function copy(x, y, time) {
$.ajax
方法$x
、$y
或$time
或不为null但不为空
编辑2:
<script src="jquery.js"></script>
results = new Array();
function copy(x, y, time) {
var o = { 'x': x, 'y': y, 'time': time };
results.push(o);
}
function save() {
var encoded_results = JSON.stringify(results);
$.ajax({
url: "process.php",
type: 'POST',
data: {
"results" : encoded_results
},
success: function(data, status, xhr) {
alert(data);
console.log(data);
console.log(xhr);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
if(isset($_POST["results"]))
{
$result_json = $_POST["results"];
$JSONArray = json_decode($result_json, true);
if($JSONArray !== null)
{
$x = $JSONArray["x"];
$y = $JSONArray["y"];
$time = $JSONArray["time"]
}
}
好的-使用json_last_error()我看到是我的json
“语法错误:格式不正确”。但我不知道如何更好地编码它
做
所以我在$u帖子上添加了一个stripslashes()来作弊。
<script src="jquery.js"></script>
results = new Array();
function copy(x, y, time) {
var o = { 'x': x, 'y': y, 'time': time };
results.push(o);
}
function save() {
var encoded_results = JSON.stringify(results);
$.ajax({
url: "process.php",
type: 'POST',
data: {
"results" : encoded_results
},
success: function(data, status, xhr) {
alert(data);
console.log(data);
console.log(xhr);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
if(isset($_POST["results"]))
{
$result_json = $_POST["results"];
$JSONArray = json_decode($result_json, true);
if($JSONArray !== null)
{
$x = $JSONArray["x"];
$y = $JSONArray["y"];
$time = $JSONArray["time"]
}
}
“x\:90,“y\”:90,“y\”:202,,“时间戳”教师教师们:10 10“x\,,“y\”:10 10,”y\::202,,“时间戳”教师教师们::“,,,“x\”::10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10,”10“x\”:10月10 10 10 10 10万万;10月10万;10万;10月10 10万;x\:10万:10万:10万;10万;10万;10万;10月10万:90,,,,,,“x\”:90,,“x\:90,,“y\:90,,“y\:90,,,“y\:90,,,“y\:90,,,,“y\”:90,,,“y\”:90,,,,,,,,,,“y\”:90,,“y\”:90,,,,,,,"时间戳:1349476537801","x":68,"y":174,"时间戳":1349476538478",,{\'x\':68,\'y\':175,\'timestamp\':1349476538512},{\'x\':68,\'y\':175,\'timestamp\':1349476538579},{\'x\':69,\'y\':175,\'timestamp\':1349476538678}]
编辑1:
发布的数据看起来不错(看下面),我在
“成功功能”。
[{“x”:529,“y”:97,“时间”:1349469608703},{“x”:385,“y”:331,“时间”:1349469608720},…]
JS-Side-index.php:
<script src="jquery.js"></script>
results = new Array();
function copy(x, y, time) {
var o = { 'x': x, 'y': y, 'time': time };
results.push(o);
}
function save() {
var encoded_results = JSON.stringify(results);
$.ajax({
url: "process.php",
type: 'POST',
data: {
"results" : encoded_results
},
success: function(data, status, xhr) {
alert(data);
console.log(data);
console.log(xhr);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
if(isset($_POST["results"]))
{
$result_json = $_POST["results"];
$JSONArray = json_decode($result_json, true);
if($JSONArray !== null)
{
$x = $JSONArray["x"];
$y = $JSONArray["y"];
$time = $JSONArray["time"]
}
}
还要设置dataType:属性
dataType:'json'
在您的ajaxrequest中我刚刚完成了这项工作,这是我对AJAX的语法:
$.ajax({
type: "GET",
url: "test2.php",
data: { anarray : array1 },
dataType: "json",
success:function(result){
array1 = result;
$.each(array1, function(x, valu){
$('#arraycontent').hide().append(x + " " + valu + "<br>").fadeIn();
})
}
});
$.ajax({
键入:“获取”,
url:“test2.php”,
数据:{anarray:array1},
数据类型:“json”,
成功:函数(结果){
数组1=结果;
$。每个(数组1,函数(x,值){
$('#arraycontent').hide().append(x+“”+valu+“
”).fadeIn();
})
}
});
PHP文件simple推送了两个元素进行演示:
<?php
$testArray = $_REQUEST['anarray'];
array_push($testArray, 'test', 'stuff');
echo json_encode($testArray);
?>
$JSONArray
是关联数组的数组,因此要访问它的第一个元素,请使用$JSONArray[0]['x]
,$JSONArray[0]['y']
而不是$JSONArray['x']
在JavaScript端,您发布了一个对象数组—一个x/y/时间组合的列表。但是,PHP端忽略了列表和对象部分:
$x = $JSONArray["x"];
vs
因此,总体而言,在一个循环中可能:
foreach($JSONArray as $triple)
{
$x=$triple->x;
$y=$triple->y;
$time=$triple->time;
}
看起来你的结果=新数组;应该是结果=新数组();Firebug显示你被传递到服务器的是什么?你面临的实际问题是什么?GwynHowell复制/粘贴错误-更正,谢谢。PaulTomblin刚刚添加了这些信息-看编辑尝试,我不知道为什么,但这会产生错误。“SyntaxError:JSON解析错误:意外标识符”。