【问题标题】:request.getParameter() returns null when used against AJAX callrequest.getParameter() 用于 AJAX 调用时返回 null
【发布时间】:2013-01-18 20:16:30
【问题描述】:

我正在使用 AJAX 调用来调用 jsp:

        var httpRequest = null;
        if (window.XMLHttpRequest) {
            httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (!httpRequest) {
            console.error('Cannot create an XML HTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() {
            try {
                if(httpRequest.readyState === 4) {
                    ...
                }
            } catch(e) {
                args.error(e);
            }
        };
        httpRequest.open(args.method, args.path, args.sync);
        httpRequest.setRequestHeader(...);
        var q = '', first = true;
        for(var key in args.params) {
            if(params.hasOwnProperty(key)) {
                if(first) {
                    first = false;
                } else {
                    q += '&';
                }
                q += encodeURIComponent(key) + '=' + encodeURIComponent(args.params[key]);
            }
        }
        httpRequest.send(q);

对于这个请求,我将查询参数传递为:

{
    from: 'xyz',
    to: 'abc'
}

正在构建的查询也是正确的:

from=xyz&to=abc

然而,在我的 JSP 上,当我执行 request.getParameter("from") 时,我得到了 null。我应该如何解决这个问题?

【问题讨论】:

    标签: ajax jsp xmlhttprequest request


    【解决方案1】:

    找到解决方案:

    缺少标题: httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    这是requests发帖所必需的。

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2021-01-27
      相关资源
      最近更新 更多