【发布时间】: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