繁体   English   中英

将ajax变量写入txt文件

[英]Writing an ajax variable to txt file

我有一个在客户端编码的Facebook应用程序,我想将令牌存储在服务器上,以备后用

有一个名为“ token”的变量,然后我创建了一个名为“ apple”的新函数,以json格式将此变量写入txt文件。

$(document).ready(function(){

$("#submit").click(function(){

    //access token stuff
    var token = $("#link_input").val();
    //alert("Got Token: " + token + ". your application token");

    if (token.split('#access_token=')[1]) {
    var token = token.split('#access_token=')[1].split('&')[0];
    //alert(token);



function WriteToFile(apple)
    {
    $.post("save.php",{ 'token': apple },
        function(data){
            alert(data);
        }, "text"
    );
    return false;
    } 

我的PHP文件

<?php
$thefile = "new.json"; /* Our filename as defined earlier */
$towrite = $_POST["token"]; /* What we'll write to the file */
echo $towrite;
$openedfile = fopen($thefile, "w");
$encoded = json_encode($towrite);
fwrite($openedfile, $encoded);
fclose($openedfile);
return "<br> <br>".$towrite;

?>

但我无法写任何东西

您必须首先在该位置创建文件,然后设置正确的权限,否则PHP将无法编写该文件。

就目前而言,您可以在JS中定义WriteToFile函数,但从未调用它。 将您的JS更改为:

$(document).ready(function(){

    $("#submit").click(function(){

        //access token stuff
        var token = $("#link_input").val();
        //alert("Got Token: " + token + ". your application token");

        if (token.split('#access_token=')[1]) {
            var token = token.split('#access_token=')[1].split('&')[0];
            WriteToFile(token);
        }
    }



    function WriteToFile(apple) {
        $.post("save.php",{ 'token': apple },
            function(data){
                alert(data);
            }, "text");
        return false;
    }

};

要么:

$(document).ready(function(){

    $("#submit").click(function(){

        //access token stuff
        var token = $("#link_input").val();
        //alert("Got Token: " + token + ". your application token");

        if (token.split('#access_token=')[1]) {
            var token = token.split('#access_token=')[1].split('&')[0];
            $.post("save.php",{ 'token': token }, function(data){
                    alert(data);
                }, "text");
        }
    }

};

暂无
暂无

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

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