<h1>编辑NTP配置文件</h1> <hr> <button id = "openBtn">打开文件</button> <button id = "saveBtn">保存</button> <br><br> <textarea id="fileContent" cols="80" rows="30"></textarea> <script> $("#openBtn").click(function(){ $.ajax({ url:"ntp1.php", type:"GET", success:function(response){ var fc = response; $("#fileContent").val(fc); } }); }); $("#saveBtn").click(function(){ var fileContent = $("#fileContent").val(); $.ajax({ url:"ntp1.php", type:"POST", data:{content:fileContent}, success:function(response){ alert("File saved successfully!"); } }); }); </script> 修改代码 帮我将ajax获取的值 不从php的echo中取
时间: 2024-02-10 11:05:58 浏览: 36
好的,这里是修改后的代码:
```
<h1>编辑NTP配置文件</h1>
<hr>
<button id="openBtn">打开文件</button>
<button id="saveBtn">保存</button>
<br><br>
<textarea id="fileContent" cols="80" rows="30"></textarea>
<script>
$("#openBtn").click(function() {
$.ajax({
url: "ntp1.php",
type: "GET",
success: function(data) {
var fc = data.content; // 从响应数据的 content 属性中获取值
$("#fileContent").val(fc);
}
});
});
$("#saveBtn").click(function() {
var fileContent = $("#fileContent").val();
$.ajax({
url: "ntp1.php",
type: "POST",
data: {content: fileContent},
success: function(response) {
alert("File saved successfully!");
}
});
});
</script>
```
在这个修改后的代码中,我们假设后端返回的数据是一个包含 `content` 属性的 JSON 对象。因此,在获取响应数据时,我们需要从 `data.content` 中获取值,而不是直接从 `response` 中获取。