我怎样才能“抓住”表格的回复?

时间:2018-01-18 09:16:09

标签: javascript html post request

我在formJS制作了POST request,一切正常,但我需要抓住该表单的响应,将其用作var in另一部分。

我该怎么做?

<!DOCTYPE html>
<html>
<head>
    <title>Basic Embed</title>

    <script type="text/javascript" 
        src="http://myserver/javascripts/api/tableau-2.min.js"></script>
    <script type="text/javascript">
        function post(path, method) {
            method = method || "post";
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);

            var username = document.createElement("input");
            username.setAttribute("type", "hidden");
            username.setAttribute("name", "username");
            username.setAttribute("value", "user");
            form.appendChild(username);

            var server = document.createElement("input");
            server.setAttribute("type", "hidden");
            server.setAttribute("name", "server");
            server.setAttribute("value", "myserver");
            form.appendChild(server);

            var client_ip = document.createElement("input");
            client_ip.setAttribute("type", "hidden");
            client_ip.setAttribute("name", "client_ip");
            client_ip.setAttribute("value", "");
            form.appendChild(client_ip);

            var target_site = document.createElement("input");
            target_site.setAttribute("type", "hidden");
            target_site.setAttribute("name", "target_site");
            target_site.setAttribute("value", "site");
            form.appendChild(target_site);

            document.body.appendChild(form);
            form.submit();

        }

    </script>
</head>

<body onload="post('http://myserver/trusted');">
    <script type='text/javascript' src='http://myserver/javascripts/api/viz_v1.js'></script>
    <div class='tableauPlaceholder' style='width: 1000px; height: 977px;'>
        <object class='tableauViz' width='1000' height='977' style='display:none;'>
            <param name='host_url' value='http://myserver/' /> 
            <param name='site_root' value='&#47;t&#47;site' />
            <param name='path' value='trusted/**THE RESPONSE SHOULD GO HERE**/t/site'/>
        </object>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:-1)

您可以在表单按钮中添加一个简单的Onclick,并将其用作普通按钮 - 用户交互

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
First name: <input type="text" name="FirstName" value="Mickey" id="NameInput"><br>
Last name: <input type="text" name="LastName" value="Mouse" id="LastNameInput"><br>
<input type="submit" value="Submit" onclick="FunctionJS()">
</form>
<script>
function FunctionJS()
{
    var name = document.getElementById("NameInput").value;
    var lastName = document.getElementById("LastNameInput").value;
    alert("name: " + name + " lastName: " + lastName);
}
</script>

</body>
</html>