I'm sending following Ajax request to mypage.jsp
jQuery.ajax({
type: "POST",
url: "../mypage.jsp",
data: {
param1: 'aaa',
param2: 'bbb'
},
contentType: "text/html; charset=utf-8",
dataType: "text",
success: function (data) {
alert("Data Loaded: " + data);
}
});
In my mypage.jsp
page, I'm trying to access param1
as below.
String result = request.getParameter("param1");
but the result
becomes null
.
However, when I change my ajax request as below, I get 'aaa'
as the result (which is the desired output).
jQuery.ajax({
type: "POST",
url: "../mypage.jsp?param1=aaa¶m2=bbb",
data: {},
contentType: "text/html; charset=utf-8",
dataType: "text",
success: function (data) {
alert("Data Loaded: " + data);
}
});
Am I using the correct method to access the data being sent by the Ajax request?
I referred some docs on Implicit Request (HTTPServletRequest) object, but could not think of any other suitable method to access data other than 'request.getParameter()'
How do I access param1
without sending it in the Ajax request URL?
Note: I also encountered a very similar SO question 'How to receive data sent by Ajax in a .jsp file' but it's not using a 'data' field as I do, hence thought to ask this question.