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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?