我使用jsp页面中的ajax发送json,如下所示:
function dateRCA()
{
var $tipUtilizare = $("select[name=tipUtilizare]").val();
var $dataRCA = $("input[name=dataRCA]").val();
var jsonObj = {"tipUtilizare": $tipUtilizare,
"dataRCA": $dataRCA};
alert($tipUtilizare);
alert($dataRCA);
$.ajax
(
{
url: 'AsigurareRCA',
data: {dateRCA: JSON.stringify(jsonObj)},
dataType: 'json',
type: 'post',
cache: false,
success: function (raspuns) {
alert("succes");
if (raspuns.status.succes === "true") {
$("#dateAsigurareRCATab").removeClass("active");
$("#dateAsigurareRCATab").addClass("disabled");
$("#dateAsigurareCASCOTab").removeClass("disabled");
$("#dateAsigurareCASCOTab").addClass("active");
$("#dateAsigurareRCA").removeClass("active");
$("#dateAsigurareRCA").removeClass("in");
$("#dateAsigurareCASCO").addClass("active");
$("#dateAsigurareCASCO").addClass("in");
}
else {
//Handlde errors
}
},
error: function () {
alert('Ne cerem scuze, dar a aparut e eroare de sistem!');
}
}
);
}
我的jsp文件中使用这些函数的表单是:
<div class="tab-pane fade" id="dateAsigurareRCA">
<div class="mainbox ">
<div class="panel panel-info">
<div class="panel-body" >
<form name="dateAsigurareRCA" action="AsigurareRCA" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label for="tipUtilizare" class="col-md-6 control-label">Tip utilizare autovehicul</label>
<div class="col-md-6">
<div class="col-md-6">
<select class="form-control" name="tipUtilizare" id="tipUtilizare">
<option value="">Selecteaza tipul de utilizare</option>
</select>
<span style="color:red; display: none" class="eroareTipUtilizare"> </span>
</div>
</div>
</div>
<div class="form-group" id="event_period">
<label for="dataRCA" class="col-md-6 control-label">Data</label>
<div class="col-md-6">
<div class="col-md-6">
<div class="input-group">
<input id="datepicker" type="text" class="date-picker form-control" name="dataRCA" value="" readonly/>
</label>
</div>
</div>
</div>
</div>
<div class="form-group">
<!-- Button -->
<div class="col-md-offset-3 col-md-9">
<input id="btn-signup" type="submit" class="btn btn-info" name="nextAsigurareCASCO" value="Continuare" onclick="dateRCA();">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" id="dateAsigurareCASCO">
Date asigurare casco
</div>
因此,如果servlet告诉我信息是正确的,我需要更改选项卡并显示下一个div。
这是来自servlet的post方法,它处理收到的数据(我使用GSON来处理JSON):
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
Gson gson = new Gson();
synchronized (getServletContext()) {
ServletContext context = getServletContext();
ConnectionPool cp = (ConnectionPool) context.getAttribute("CONNECTION_POOL");
System.out.println("ce am preluat " + request.getParameter("dateRCA"));
String tipUtilizare = request.getParameter("tipUtilizare");
String dataRCA = request.getParameter("dataRCA");
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
System.out.println("parametru name " + paramName);
String[] paramValues = request.getParameterValues(paramName);
for (int i = 0; i < paramValues.length; i++) {
String paramValue = paramValues[i];
System.out.println("parametru value " + paramValue);
}
}
HttpSession session = request.getSession();
Connection con;
Map<String, Map<String, Object>> mesaj = new LinkedHashMap<>();
Map<String, Object> status = new LinkedHashMap<>();
status.put("succes", "true");
mesaj.put("status", status);
response.getOutputStream().print(gson.toJson(mesaj));
response.getOutputStream().flush();
}
}
问题是request.getParameter(&#34; dateRCA&#34;)返回null。我从jsp页面获得的是以下内容(这些是请求参数及其值):
Info: param name tipUtilizare
Info: param value Paza si protectie
Info: param name dataRCA
Info: param value 06/16/2015
Info: param name nextAsigurareCASCO
Info: param value Continuare
它甚至可以给我按钮的价值,即使我不是从ajax发送它......
我从servlet得到的是一个有效的JSON:{&#34; status&#34;:{&#34; succes&#34;:&#34; true&#34;}},但不是改变活动选项卡和div,它将我重定向到servlet链接,向我显示发送的JSON对象。
我使用相同的方法处理其他一些形式并且效果很好..经过几个小时试图找出解决这个问题的方法......并且没有理由说它为什么不起作用它应该..我问你,如果你能看到问题是什么,我该如何解决呢。