我正在使用 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
. 我应该如何解决这个问题?