JavaScript:帖子无法发送参数?

时间:2014-06-09 20:48:43

标签: javascript html http post

在我的网页中,我尝试通过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>

3 个答案:

答案 0 :(得分:1)

 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();

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

 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();

答案 1 :(得分:0)

你错过了form元素中最重要的事情...... form它自己。

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

答案 2 :(得分:0)

您应该在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