好的,我花了很多時間在這,我不明白我做錯了什麼。
似乎不可能在PHP文件中獲取數據。
- 首先我多次調用「複製」來填充「結果」數組。
- 然後,我稱之爲
$.ajax
方法 - 在process.php $ _ POST是空
- >在PHP $x
,$y
或$time
或不爲空而不空。
Edit 2:
Ok - with json_last_error() i saw that it's my json which is "Syntax error: malformed". But i don't know how to encode it better than what i'm doing.
So i cheat by adding a stripslashes() on the $_POST.
[{\"x\":104,\"y\":218,\"timestamp\":1349476537434},{\"x\":90,\"y\":202,\"timestamp\":1349476537469},{\"x\":82,\"y\":192,\"timestamp\":1349476537487},{\"x\":71,\"y\":177,\"timestamp\":1349476537514},{\"x\":68,\"y\":174,\"timestamp\":1349476537568},{\"x\":68,\"y\":173,\"timestamp\":1349476537801},{\"x\":68,\"y\":174,\"timestamp\":1349476538478},{\"x\":68,\"y\":175,\"timestamp\":1349476538512},{\"x\":68,\"y\":175,\"timestamp\":1349476538579},{\"x\":69,\"y\":175,\"timestamp\":1349476538678}]
Edit 1:
The posted data seems to be good (look under), and i finish in the "success function".
[{"x":529,"y":97,"time":1349469608703},{"x":385,"y":331,"time":1349469608720},.....]
JS方 - 的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);
}
});
}
PHP端 - process.php:
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"]
}
}
看起來像你的結果=新的陣列;應該是results = new Array(); –
Firebug顯示你傳遞給服務器的是什麼? –
你面臨的實際問題是什麼? – rlemon