I've been trying to get something to work with AJAX/jQuery and Servlets, and I keep getting syntax errorrs and the browser doesn't show where the error lies.
Here's the code.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#btnTest").click(function(){
var n = $("#vName").val();
$.get("TestServlet", (nm:n), function(responseText){
$("#textArea").text(responseText);
});
});
});
</script>
<body>
<h1>Hello World!</h1>
<input type="text" id="vName" value="" />
<input type="button" value="Ok" id="btnTest" />
<div id="textArea"></div>
</body>
And then in my servlet I have this ;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String val = request.getParameter("mn");
String msg = "Hi User";
if(val!=null)
msg = "Hi, " + val;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getWriter().write(msg);
}
finally
{
out.close();
}
}
The (nm:n)
in your call to $.get
is invalid syntax. You probably meant {nm:n}
:
$.get("TestServlet", {nm:n}, function(responseText){
// Here -------------^----^
错误:
$.get("TestServlet",{ nm:n }, function(responseText)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.