繁体   English   中英

JavaScript:Post无法发送参数?

[英]JavaScript: Post can't send parameters?

在我的网页中,我尝试通过JavaScript发送参数。

当我收到请求时,我发现参数为空? 我很确定,因为我可以收到请求,所以请求可以成功发送。

我不知道为什么这是错误的,这是我的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Expires" content="0">
<meta http-equiv="kiben" content="no-cache">
<title>Login Page</title>
<script type="text/javascript">
function GetCookie (name) 
{ 
    var arg = name + "="; 
    var alen = arg.length; 
    var clen = window.document.cookie.length; 
    var i = 0; 
    while (i < clen) 
    { 
        var j = i + alen; 
        if (window.document.cookie.substring(i, j) == arg) return getCookieVal (j); 
        i = window.document.cookie.indexOf(" ", i) + 1; 
        if (i == 0)
            break; 
    } 
    return null;
}
function getCookieVal (offset)
{ 
    var endstr = window.document.cookie.indexOf (";", offset); 
    if (endstr == -1)
        endstr = window.document.cookie.length; 
    return unescape(window.document.cookie.substring(offset, endstr));
}
function SetCookie (name, value)
{ 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + (30*24*60*60*1000));
    window.document.cookie = name + "=" + escape (value) + "; expires=" + exp.toGMTString()+";path=/";
}
function DeleteCookie (name)
{ 
    var exp = new Date(); 
    exp.setTime (exp.getTime() - 100); 
    var cval = GetCookie (name); 
    window.document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()+";path=/";
}

function DelCookie()
{
    DeleteCookie(document.getElementById("username").value);
}
function remember()
{
    if(document.getElementById("remember").checked){
        SetCookie(document.getElementById("username").value,document.getElementById("password").value);
        alert("Saved!");
    }   
    createForm();
}
function showpassword()
{
     var p=GetCookie(document.getElementById("username").value);
     if(p!=null)
    document.getElementById("password").value= p;
}
function createForm(){
     var form = document.createElement("form");
     form.method="post";
     form.action="login";
     var usernmae = document.getElementById("username");
     var pwd = document.getElementById("password");
     var hiddenField = document.createElement("input");
     hiddenField.setAttribute("type", "text");
     hiddenField.setAttribute("username",username.value);
     hiddenField.setAttribute("password",pwd.value);
     form.appendChild(hiddenField);  
     document.body.appendChild(form);
     form.submit();
}
</script>
</head>
<body>

        UserName:<input type="text" id="username" onblur="showpassword()"><br />
        Password:<input type="password" id="password">
        <input type="checkbox" name="remember" id="remember">
        </input>Remeber Username<br />
        <input value="Submit" type="button" onClick="remember()"> 
        <input value="Delete" type="button" onClick="DelCookie()">
</body>
</html>
 var hiddenField = document.createElement("input");
     hiddenField.setAttribute("type", "text");
     hiddenField.setAttribute("username",username.value);
     hiddenField.setAttribute("password",pwd.value);
     form.appendChild(hiddenField);  
     document.body.appendChild(form);
     form.submit();

发送这样的参数是不可能的,通过提交的每个html控件上的表单提交需求name attribute发送数据

 var hiddenField = document.createElement("input");
         hiddenField.setAttribute("type", "text");
         hiddenField.setAttribute("name","username");
         hiddenField.setAttribute("value",username.valuee);
         form.appendChild(hiddenField);

var hiddenField2 = document.createElement("input");
         hiddenField2.setAttribute("type", "text");
         hiddenField2.setAttribute("name","password");
         hiddenField2.setAttribute("value",pwd.value);
         form.appendChild(hiddenField2);

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

你缺少的最重要的form元素..的form它的自我。

如何将输入包装在大的<form...>子句中。

您应该在HTML中使用<form>标记来执行此操作,而不是通过编程方式创建表单。

<body>
    <form action="login" method="POST">
        UserName:<input type="text" id="username" onblur="showpassword()"><br />
        Password:<input type="password" id="password">
        <input type="checkbox" name="remember" id="remember">
        </input>Remeber Username<br />
        <input value="Submit" type="button" onClick="remember()"> 
        <input value="Delete" type="button" onClick="DelCookie()">
    </form>
</body>

如果要这样做(在JavaScript中),则应使用createElement创建2个input ,而不是一个input

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM