我正在尝试使用Ajax在JSP页面上发送用户输入的数据。我已经使用Ajax在Jsp页面上发送了变量“ vars”。 输出: 注册成功。欢迎使用null,您的密码为:null,名字:null,姓氏:null,电子邮件:null,出生日期为:null,
var output1 = document.getElementById("output1");
function saveUserInfo() {
var userName = document.getElementById('username').value;
var password = document.getElementById('password').value;
var firstName = document.getElementById('firstname').value;
var lastName = document.getElementById('lastname').value;
var email = document.getElementById('email').value;
var dob = document.getElementById('datepicker').value;
var vars = "username"+userName+"&password"+password+"&firstname"+firstName+"&lastname"+lastName+"&email"+email+"&datepicker"+dob;
var hr = new XMLHttpRequest();
var url = 'register.jsp';
hr.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
console.log(hr.responseText);
output1.innerHTML = (hr.responseText);
}
}
hr.open("POST",url,true);
hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
hr.send(vars);
}
这是我的JSP页面:我已经尝试过这种方式,但是,如何使用变量“ vars”发送的信息呢?
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user = request.getParameter("username");
session.putValue("username",user);
String pwd = request.getParameter("password");
String fname = request.getParameter("firstname");
String lname = request.getParameter("lastname");
String email = request.getParameter("email");
String dob = request.getParameter("dateofbirth");
%>
Registration is Successfull. Welcome
<%=user %>,
Your Password is : <%=pwd %>,
FirstName : <%=fname %>,
LastName : <%=lname %>,
Email : <%=email %>,
and Date Of Birth is : <%=dob %>,
我试图在不重新加载页面的情况下在同一页面上显示信息。
答案 0 :(得分:1)
代码中的问题是,当您传递变量时,您没有使用“ = ”。该运算符用于将值分配给某些变量。因此,在您的代码中
var vars = "username"+userName+"&password"+password+"&firstname"+firstName+"&lastname"+lastName+"&email"+email+"&datepicker"+dob;
您忘记给变量赋值时给“ = ”,只需将其更改为类似
var vars = "username="+userName+"&password="+password+"&firstname="+firstName+"&lastname="+lastName+"&email="+email+"&datepicker="+dob;
有关更多信息,请参阅此-http://www.javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml