javascript ajax servlet表单参数名称错误

时间:2017-06-28 07:50:59

标签: javascript ajax servlets

我正在使用javascrpt动态创建一个html表单并使用ajax提交它。当我从servlet检查参数名称时,它没有正确显示参数名称。下面显示了javascript / ajax和servlet代码以及tomcat日志文件(catalina.out)的诊断输出

使用Javascript:

var gulticsForm = document.createElement("form");
gulticsForm.action="/gultics/Utils";
var element1=document.createElement("input");
element1.setAttribute("name", "sqlText");
element1.setAttribute("value", "This is the value of the element");
gulticsForm.appendChild(element1);
document.body.appendChild(gulticsForm);
var fm = new FormData(gulticsForm);
xhttp.open("POST", "/gultics/Utils", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhttp.send(fm);

的Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response) { callServer(request, response); }
public void doGet(HttpServletRequest request, HttpServletResponse response) { callServer(request, response); }
private void callServer(HttpServletRequest request, HttpServletResponse response) {
    try {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        Enumeration<String> parameterNames = request.getParameterNames();
        int parameterNo = 0;
        while (parameterNames.hasMoreElements()) {
            System.out.println("Name of Parameter " + parameterNo++ + " is " + parameterNames.nextElement());
        }
        String sqlText = request.getParameter("sqlText");
        System.out.println("SQL Text: " + sqlText);

来自catalina.out的输出

参数0的名称是----------------------------- 229583121529757

内容 - 处置:表单数据;名称

SQL Text:null

你能帮帮忙吗?如果我能提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

你必须阅读请求的正文,看看这个答案

Getting request payload from POST request in Java servlet