将LocalStorage JSON传递给服务器上的文本文件

问题描述 投票:0回答:2

目标:将驻留在localStorage中的JSON字符串发送到服务器上的文本文件。

我尝试使用AJAX和JQuery。 Link1 Link2

我的代码:

JAVA脚本

function testAPI() {
                FB.api('/me?fields=id,name,email', function(response) {
                    localStorage.user_data=JSON.stringify(response);

                        $.ajax({
                                    type: "POST",
                                url: "https://vtpt.in/~ad/ignore/backend.php",
                            data: {localStorage.user_data},
                        success: function() {alert("Data Sent");}
                    });


                });
    }

PHP

Below is the data retrieved from SERVER

<?php
    $dataObject = $_POST['data']; //Fetching all posts
    file_put_contents('user_data.txt', $dataObject , FILE_APPEND | LOCK_EX );

?>
php json ajax local-storage
2个回答
0
投票
$dataObject = $_POST['data']; //Fetching all posts
var_dump($_POST);//Verify you get the content
$result = file_put_contents('user_data.txt', $dataObject , FILE_APPEND | LOCK_EX );
if (!$result) {
    var_dump(error_get_last());//prints last php last error
}

0
投票

问题是我的服务器没有安装PHP。安装PHP删除了我没有在文件系统级别进行更改的问题,即没有创建文件。但现在工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.