我试图在post中将数据从servlet获取到ajax,但它总是会出现错误部分,就像“未定义”这样对ajax kinldy这个新手来说是新手。 插入部分工作正常,但我试图获取值去错误部分??
的servlet
package Servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Databases.databases;
/**
* Servlet implementation class Form
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/Form" })
public class Form extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Form() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("sssssssss");
//System.out.println("input are====>"+input);
//response.setContentType("text/plain;charset=UTF-8");
response.setContentType("text/html");
String name=request.getParameter("name");
String job=request.getParameter("job");
System.out.println("name===>"+name);
System.out.println("job===>"+job);
ArrayList al=new ArrayList();
String a="";
String b="";
//databases d=new databases();
try {
Connection con=databases.getconnection();
String sql="insert into senthiil(name,job)values(?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, job);
System.err.println("sql===>"+ps);
// ps.execute();
//con.commit();
String select="select name,job from senthiil";
PreparedStatement psselect=con.prepareStatement(select);
System.out.println("select======>"+psselect);
ResultSet rs=psselect.executeQuery();
while(rs.next())
{
al.add(rs.getString(1));
al.add(rs.getString(2));
System.out.println("al====>"+al);
request.setAttribute("data", al);
/*a=rs.getString(1);
b=rs.getString(2);
*/
}
//response.getWriter().write(a);
response.getWriter().write("{issue:true}");
response.getWriter().print(al);
response.getWriter().close();
}
catch (ClassNotFoundException | SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("e");
}
}
}
JS:
$(document).ready(function()
{
$("#submit").click(function(){
alert("senthil");
var name=$("#name").val();
var job=$("#job").val();
// var datastring="name" + + "&job=" + job; // option one type:'post',
//alert("datastring=======>"+datastring);
$.ajax({
url: "Form",
data: {"name": name, "job": job},
dataType: 'text/html',
type: 'post',
success: function(data)
{
alert("ssssss");
alert("data===>"+data);
console.log(data);
},
error: function(e) {
alert('Error: ' + e.message);
}
});
});
});
答案 0 :(得分:0)
您应该将数据格式化为json类型,以便客户端可以在错误的情况下接收它。在您的服务器端,请更改您的响应内容输入:“application / json”
response.setContentType("application/json");
在你的ajax调用中,将dataType更改为json,并更改控制台日志以检查它是否有效:
dataType: 'json',
alert("data===>"+data.issue);
您应该将a1 List格式化为json字符串并将该字符串附加到response.getWriter().write("{issue:true}");
的响应对象,此库可以帮助您将java对象解析为json字符串: