我在JQuery中使用Ajax调用将一些数据发送到JSP文件。这是代码:
$.post("myscript.jsp",
{ sg: data1, st: data2 },
function(data){
alert("Data Loaded: " + data);
}
);
我的问题是,如何在myscript.jsp中接收数据? 我需要数据在jsp中执行一些计算。
答案 0 :(得分:2)
您可以从请求获取参数,如:
String data1 = request.getParameter("sg");
String data2 = request.getParameter("st");
答案 1 :(得分:1)
试试这个
var params ="your parameters to send";
var resultStringX = $.ajax({
type: "POST",
url:"myscript.jsp",//jsp,servlet,struts action
data: params,
async: false
}).responseText;
resultStringX=$.trim(resultStringX);
//here the result will be stored in resultStringX
或
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#call').click(function ()
{
$.ajax({
type: "post",
url: "myscript.jsp", //here you can use servlet,jsp, etc
data: "input=" +$('#ip').val()+"&output="+$('#op').val(),
success: function(msg){
$('#output').append(msg);
}
});
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
input:<input id="ip" type="text" name="" value="" /><br></br>
output:<input id="op" type="text" name="" value="" /><br></br>
<input type="button" value="Call JSP" name="Call JSP" id="call"/>
<div id="output"></div>
</body>