如何使用javascript向服务器提交数据
2个回答
推荐于2017-10-07 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517198
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
第一种方式是,点击链接,触发一个js函数,在该函数内,用dom生成表单和输入框,将值赋在表单里,提交表单。
[javascript] view
plaincopyprint?
function postwith(to, p) {
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = to;
for ( var k in p) {
var myInput = document.createElement("input");
myInput.setAttribute("name", k);
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
function postwith(to, p) {
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = to;
for ( var k in p) {
var myInput = document.createElement("input");
myInput.setAttribute("name", k);
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
[html] view
plaincopyprint?
<body>
<a href="javascript:postwith('save',{'currentPage':'2','xisuo':'计算机'})">use js to post</a>
<body>
<a href="javascript:postwith('save',{'currentPage':'2','xisuo':'计算机'})">use js to post</a>
save是个servlet
[java] view
plaincopyprint?
public class save extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String currentPage = request.getParameter("currentPage");
String xisuo = request.getParameter("xisuo");
System.out.println(currentPage+"---"+xisuo);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method1111111111");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public class save extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String currentPage = request.getParameter("currentPage");
String xisuo = request.getParameter("xisuo");
System.out.println(currentPage+"---"+xisuo);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method1111111111");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
第二种是用表单
[html] view
plaincopyprint?
<body>
<form name="form1" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form2" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form3" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
</body>
<body>
<form name="form1" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form2" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form3" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
</body>
[javascript] view
plaincopyprint?
function postData(){
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = "post-data-result.jsp";
var inputs=document.getElementsByName("post_data");
var i;
for (i=0;i<inputs.length;i++) {
var myInput = document.createElement("input");
myInput.type = "text";
myInput.name="post_data";
myInput.value=inputs[i].value;
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
function postData(){
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = "post-data-result.jsp";
var inputs=document.getElementsByName("post_data");
var i;
for (i=0;i<inputs.length;i++) {
var myInput = document.createElement("input");
myInput.type = "text";
myInput.name="post_data";
myInput.value=inputs[i].value;
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
[html] view
plaincopyprint?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>获得客户端数据</title>
</head>
<body>
<%
//这里设置的字符集要跟index.html中的charset一致
request.setCharacterEncoding("utf-8");
String[] text=request.getParameterValues("post_data");
for(String t : text){
out.print(t+"<br>");
}
%>
</body>
</html>
[javascript] view
plaincopyprint?
function postwith(to, p) {
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = to;
for ( var k in p) {
var myInput = document.createElement("input");
myInput.setAttribute("name", k);
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
function postwith(to, p) {
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = to;
for ( var k in p) {
var myInput = document.createElement("input");
myInput.setAttribute("name", k);
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
[html] view
plaincopyprint?
<body>
<a href="javascript:postwith('save',{'currentPage':'2','xisuo':'计算机'})">use js to post</a>
<body>
<a href="javascript:postwith('save',{'currentPage':'2','xisuo':'计算机'})">use js to post</a>
save是个servlet
[java] view
plaincopyprint?
public class save extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String currentPage = request.getParameter("currentPage");
String xisuo = request.getParameter("xisuo");
System.out.println(currentPage+"---"+xisuo);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method1111111111");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public class save extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String currentPage = request.getParameter("currentPage");
String xisuo = request.getParameter("xisuo");
System.out.println(currentPage+"---"+xisuo);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method1111111111");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
第二种是用表单
[html] view
plaincopyprint?
<body>
<form name="form1" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form2" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form3" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
</body>
<body>
<form name="form1" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form2" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
<form name="form3" action="post-data-result.jsp" method="post">
<input name="post_data" type="text"></input>
<input type="button" onclick="postData()" value="提交"></input>
</form>
</body>
[javascript] view
plaincopyprint?
function postData(){
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = "post-data-result.jsp";
var inputs=document.getElementsByName("post_data");
var i;
for (i=0;i<inputs.length;i++) {
var myInput = document.createElement("input");
myInput.type = "text";
myInput.name="post_data";
myInput.value=inputs[i].value;
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
function postData(){
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = "post-data-result.jsp";
var inputs=document.getElementsByName("post_data");
var i;
for (i=0;i<inputs.length;i++) {
var myInput = document.createElement("input");
myInput.type = "text";
myInput.name="post_data";
myInput.value=inputs[i].value;
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
[html] view
plaincopyprint?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>获得客户端数据</title>
</head>
<body>
<%
//这里设置的字符集要跟index.html中的charset一致
request.setCharacterEncoding("utf-8");
String[] text=request.getParameterValues("post_data");
for(String t : text){
out.print(t+"<br>");
}
%>
</body>
</html>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询