ajax传递值到servlet问题
我用ssh框架写了个小项目,在jsp页面给一个超链接定义两个id号,id="1" coId="2"
我想把两个id号传到servlet里去。使用ajax这么做的
function setProid(o) {
createXMLHttpRequest();
if (xmlrequest) {
//准备发送请求
var req = "/test/ChangeUniversity?proid="+o.id+"&cid="+o.coId;
//打开请求
xmlrequest.open("get", req, true);
//指定处理结果的函数
alert(req);
//发送请求
xmlrequest.send();
}
}
servlet 代码
public class ChangeUniversity extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
//ie禁用缓存
response.setHeader("Cache-Control", "no-cache");
String proid=request.getParameter("proid");
String cid=request.getParameter("cid");
System.out.println(proid);
System.out.println(cid);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
点击超链接的时候弹出 /test/ChangeUniversity?proid=1&cid=2 但是后台得不到数据 我刚入门 不知道是什么原因
请高手解答 不胜感激
------解决方案--------------------
response.setContentType("text/xml;charset=utf-8");
改成
response.setContentType("text/html;charset=utf-8");
测试
var req = "/test/ChangeUniversity?proid="+o.id+"&cid="+o.coId + "&xx=" + (new Date()).valueOf();
//打开请求
xmlrequest.open("GET", req, true);
//指定处理结果的函数
alert(req);
//发送请求
xmlrequest.onreadystatechange = function(){
if (xmlrequest.readyState != 4) { return; }
if (xmlrequest.status != 200) {
return;
}
alert(xmlrequest.responseText);
};
xmlrequest.send(null);
System.out.println(proid);
System.out.println(cid);
改成
PrintWriter out = response.getWriter();
out.print(proid);
out.print(cid);
System.out.println没有输出到页面中