繁体   English   中英

AJAX不通过PHP将数据发布到文本文件

[英]AJAX not posting data to text file via PHP

我有一个表单,提交后将条目保存到服务器上的文本文件中。

我正在使用的jQuery是:

$(document).ready(function(){
localStorage.clear();

$("form").on("submit", function() {
    if(window.localStorage!==undefined) {
        var fields = $(this).serialize();

        localStorage.setItem("eloqua-fields", JSON.stringify( fields ));
        alert("Stored Succesfully");
        $(this).find("input[type=text]").val("");
        alert("Now Passing stored data to Server through AJAX jQuery");
        $.ajax({
           type: "POST",
           url: "backend.php",
           contentType: 'application/json; charset=utf-8',
           data: {data: fields},
           success: function(data) {
              $('#output').html(data);
           }
        });
    } else {
        alert("Storage Failed. Try refreshing");
    }
});
});

我正在使用的PHP是:

 <h1>Below is the data retrieved from SERVER</h1>
<?php
    date_default_timezone_set('America/Chicago'); // CDT
    echo '<h2>Server Timezone : ' . date_default_timezone_get() . '</h2>';
    $current_date = date('d/m/Y == H:i:s ');
    print "<h2>Server Time : " . $current_date . "</h2>";
    $dataObject = $_POST['data'];
    $json = json_decode($dataObject);
    echo $json;
    file_put_contents('your_data.txt', $json);
?>

您可以在此处查看该表单的实时版本: http : //hackingarticles.com/marketer/

问题是我的代码没有将任何数据发布到http://hackingarticles.com/marketer/your_data.txt

我一直在努力解决这个问题,但是它对我不起作用。

任何帮助表示赞赏。

干杯

PHP:

<h1>Below is the data retrieved from SERVER</h1>
<?php
        date_default_timezone_set('America/Chicago'); // CDT
        echo '<h2>Server Timezone : ' . date_default_timezone_get() . '</h2>';
        $current_date = date('d/m/Y == H:i:s ');
        print "<h2>Server Time : " . $current_date . "</h2>";

        $dataObject = $_POST; //Fetching all posts

        echo "<pre>"; //making the dump look nice in html.
        var_dump($dataObject);
        echo "</pre>";

            //Writes it as json to the file, you can transform it any way you want
        $json = json_encode($dataObject);
        file_put_contents('your_data.txt', $json);
?>

JS:

<script type="text/javascript">
$(document).ready(function(){
    localStorage.clear();

    $("form").on("submit", function() {
        if(window.localStorage!==undefined) {
            var fields = $(this).serialize();

            localStorage.setItem("eloqua-fields", JSON.stringify( fields ));
            alert("Stored Succesfully");
            $(this).find("input[type=text]").val("");
            alert("Now Passing stored data to Server through AJAX jQuery");
            $.ajax({
               type: "POST",
               url: "backend.php",         
               data: fields,
               success: function(data) {
                  $('#output').html(data);
               }
            });
        } else {
            alert("Storage Failed. Try refreshing");
        }
    });
});
</script>

尝试在ajax请求中设置contentType属性,例如contentType: 'application/json; charset=utf-8' contentType: 'application/json; charset=utf-8' 我已经遇到了这个问题(不向服务器发送任何数据),这解决了我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM