dongtuo3795 2017-01-15 21:37
浏览 50
已采纳

通过AJAX将JavaScript数组转换为PHP

I have written a simple JS script, that saves mouse positions in an array, which I then send to a php function via AJAX. It works, and saves the recieved data, but the problem is how it is saved, i.e. i would expect to have a normal output of the x and y position as is: [x1,y1],[x2,y2],[x3,y3],...

But what i get is something like this: a:63:{i:0;a:2:{i:0;i:527;i:1;i:1010;}i:1;a:2:{i:0;i:490;i:1;i:1205;}i:2;a:2:{i:0;i:588;i:1;i:1311;}i:3;a:2:{i:0;i:615;i:1;i:1368;}i:4;a:2:{i:0;i:553;i:1;i:1474;}i:5;...

I thought if i encode it in JSON format that it would save as i thought, but i dont understand why the output is as it is. Any ideas?

The JS code is as follows:

window.onbeforeunload = function() {
  var jsonString = JSON.stringify(tabela);
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: {
            text1: jsonString
        }
    });
}

And the PHP side is this:

    $text1 = json_decode(stripslashes($_POST['text1']));
    $string_data = serialize($text1);
    file_put_contents("your-file.txt", $string_data);
    
  • 写回答

1条回答 默认 最新

  • douyalin0847 2017-01-15 21:41
    关注

    The content looks like this in the file, because you passed the array through serialize function. In order to "decode" file content, use unserialize. If you want to have more human-readable file content, just store the JSON string in the file ($_POST['text1'] directly) or instead of serialize use json_encode again before calling file_put_contents.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置