ajax异步:jsp无法传输数据到Servlet????
jsp代码程序代码:
<%@ page pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>省会查询</title> </head> <body> <script> function ajaxProcess(obj) { var xhr = new XMLHttpRequest(); var text=obj.value; var url="ajax/ajaxServlet?text="+text; xhr.onreadystatechange=function() { if(xhr.readyState==4&&xhr.status==200) { doucment.getElementById("provincial_captial").value=xhr.responseText; } }; xhr.open("GET",url,true); xhr.send(); } </script> <h1>获取省份对应的省会信息</h1> <label>输入省份</label><input type="text" id="province" onblur="ajaxProcess(this)"> <br><br> <label>对应省会</label><input type="text" id="provincial_capital"> </body> </html>
ajaxServlet.java
程序代码:
package ajax; import import import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ajaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ajaxServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String,String> map = new HashMap<String,String>(); map.put("上海市","上海"); map.put("湖北省","武汉"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String text = request.getParameter("text"); out.print(text); out.print("<br>"); String provincial_captial = map.get(text); if(provincial_captial==null) { out.print("Error"); } else { out.print(provincial_captial); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
确认过了,input的值在没有传过去?是url有问题吗??