将输入发送到 Ajax 中的文件

Send Input to a File In Ajax

我想把下面的代码放在textarea中,然后发送到一个文件中,我不知道Ajax我明白了,但是不会写。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Testing</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="main.js"></script>
</head>
<body>
    <textarea id="html"></textarea>
    <textarea id="js"></textarea>
    <textarea id="css"></textarea><br>
    <input type="button" value="Run" id="run">
    <iframe src="example.html" frameborder="0"></iframe>
    <button onclick="loadDoc()">Try The Ajax</button>
</body>
</html>

main.js的内容是:

function loadDoc() {
    var fred = document.getElementById("js");
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("html").innerHTML =
                this.responseText;
        }
    };
    xhttp.open("GET", "example.html", true);
    xhttp.send();
}

加载但不会发送。我试过 xhttp.send(fred, "example.html",true) 但没有成功。有点像 codepen or jsfiddle

Ajax 是在不离开页面的情况下从 JavaScript 发出 HTTP 请求的行为。

无法写入文件。

您可以向 URL 发出 HTTP 请求,该请求由服务器端代码处理,该代码使用请求中的信息写入文件(尽管写入数据库更为常见)。

选择一种您喜欢的编程语言并查找使用它进行服务器端编程的入门教程。