jQuery的Ajax的数据发布到本地文件本地文件、数据、jQuery、Ajax

2023-09-11 01:39:22 作者:烟花未冷心已凉ゞ

我想读本地文本文件的内容到文本区域,然后修改文本区域,随后节约修改文本区域价值成同一个本地文件。我不能使用服务器端code所以这种尝试用jQuery的Ajax POST方法。我的HTML看起来是这样的 -

I am trying to read contents of a local text file into text area, then modifying textarea and subsequently saving modified text area value into the same local file. I cannot use server side code so trying this with Jquery Ajax post method. My html looks like this -

<html>
    <head>
        <title>Edit Properties</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="./js/graph/graph.js"></script>
        <script>    
var testpath;   
var buildpath;  
var dataOnFile;
var buildnum;
    function loadFile() {
        var URL = "somepath";
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        } else {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET", URL, false);
        xhttp.send(""); 
        return xhttp.response;      
    }

    function edit(){    
    $(document).ready(function() {
    //console.log(loadFile());
        $("#area").val(loadFile());//load the contents correctly
        $("#save").click(function()
            {
                testpath = window.location;
                buildpath=(testpath+"").replace("somepath1","");
                buildpath= buildpath + "somepath2";
                dataOnFile=$("#area").val();            
                console.log(dataOnFile);//logs updated value of text area
                $.ajax({
                  type : "POST",
                  async:false,                
                  data : dataOnFile,    
                  url:buildpath,
                  dataType : "text",
                  success: function(data) {                 
                    alert("File Saved");
                  }
                });
            });
        });
    }
    </script>
<body onload="edit()">
        <p>
            <textarea rows="50" cols="100" id="area"></textarea>
            <input type='button' value='Save File' id="save"/>
        </p>
    </body>
</html>

这工作没有错误,但我的更改不会保存到文件中。任何指针,以解决此问题?

This works without error but my changes are NOT saved into a file. Any pointers to resolve this?

推荐答案

JavaScript最初被设计在拒绝访问文件系统的方式。如果你想mannupilate文件,你应该使用服务器端语言如PHP。这些教程可以帮助你得到你想要做什么:

Javascript was designed in a way that denies access to the file system. If you want to mannupilate files, you should probably use a server side language like php. These tutorials might help you get what your trying to do:

学习如何使用jQuery AJAX与PHP [视频教程]

如何保存输入值到使用jQuery 文本文件

 
精彩推荐
图片推荐