Can I use the following snippet,
var MSG = $("#message").val();
var dataString = 'message='+ MSG;
$.ajax({
type: "GET",
url: "InsertMessage?message="+MSG,
data: {message:MSG},
cache: false,
success: function(data)
{
$(".content").prepend(data);
$("#message").focus();
}
});
I want to send a parameter to servlet to add to the database data: {message:MSG},
sends an empty parameter
If the #message
element you're wanting to send the text of is just a standard div
, then you need to use text()
instead of val()
:
var MSG = $("#message").text();
val()
is mostly used for form elements such as input
, select
, etc.
Remove data. It is no longer needed since you passed the parameter in the URL in GET. Make sure the URL is accessible and the parameter is correctly handled by the Request parameter in your servlet.