【问题标题】:Jquery Ajax Post Data to Local FileJquery Ajax 将数据发布到本地文件
【发布时间】:2015-01-19 20:21:31
【问题描述】:

我正在尝试将本地文本文件的内容读入文本区域,然后修改文本区域并随后将修改后的文本区域值保存到同一个本地文件中。我不能使用服务器端代码,所以尝试使用 Jquery Ajax 发布方法。我的 html 看起来像这样 -


<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>

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

【问题讨论】:

  • 如何不使用服务器端代码更改服务器上的任何内容? :-) 你正试图通过手机发送蛋糕
  • “我不能使用服务器端代码”那么你正在尝试的东西是不可能的。
  • 是的,服务器端有什么,即使您无法更改?
  • 文件在我的本地机器上。
  • 想象一个世界,您可以通过向其 url 发布请求来更新 Internet 上的任何页面。

标签: jquery html ajax file save


【解决方案1】:

Javascript 的设计方式拒绝访问文件系统。如果你想处理文件,你可能应该使用像 php 这样的服务器端语言。本教程可能会帮助您实现您的目标:

Beginners guide to Ajax with PHP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多