JavaScript:Post-can';不发送参数?
在我的网页中,我尝试通过JavaScript发送参数 当我收到请求时,我发现参数为null? 我很确定,请求可以成功发送,因为我可以收到请求 我不知道为什么错了,这是我的代码:JavaScript:Post-can';不发送参数?,javascript,html,http,post,Javascript,Html,Http,Post,在我的网页中,我尝试通过JavaScript发送参数 当我收到请求时,我发现参数为null? 我很确定,请求可以成功发送,因为我可以收到请求 我不知道为什么错了,这是我的代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Expires" content="0"> <meta h
<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>
登录页面
函数GetCookie(名称)
{
var arg=name+“=”;
var alen=arg.length;
var clen=window.document.cookie.length;
var i=0;
而(我
密码:
记住用户名
您缺少了表单中最重要的元素。。表单本身就是这样的
如何将输入包装在一个大的
子句中。您应该在HTML中使用
标记来完成此操作,而不是通过编程方式创建表单
<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
,而不仅仅是一个
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属性
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();