无法从XMLHttpRequest获取参数

时间:2013-05-02 08:07:13

标签: ajax servlets xmlhttprequest

我可以从js看到参数,但我不能使用getParameter来获取它们。有什么问题?

js code

var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var req = new XMLHttpRequest(); 
req.open("POST", "../PersonTest", true);
req.onreadystatechange = function(){
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert(req.responseText);
        } else {
            alert("HTTP error " + req.status + ": " + req.statusText);
        }
    }
}
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
req.send("email=" + email + "&password=" + password);

servlet代码

    response.setContentType("text/xml;charset=UTF-8");  
    PrintWriter out = response.getWriter();  
    try {  
        //print request content
        ServletInputStream is = request.getInputStream();   
        ... 
        System.out.println(new String(baos.toByteArray(), "utf-8"));

        //get parameter
        String email = request.getParameter("email");  
        String password = request.getParameter("password");
        System.out.println(email + "  " + password);

    } 

servlet输出: 电子邮件= 123&安培;密码= 123 null null

0 个答案:

没有答案