无法将 json 对象发送到 servlet
Not able to send json object to servlet
我在这里通过 AJAX 从 JSP.In servlet 调用调用 servlet,从数据库中获取数据并将这些数据转换为 JSON 使用我想要的 Gson.After 格式将此 json 对象发送到此 JSP.But 此处我无法发送 json object.Please 建议我?
AJAX 调用的代码:
<script type="text/javascript">
$(document).ready(function() {
$('#subbtn').click(function(event) {
console.log("Hello");
var searchfield = $('#searchfields').val();
var operator = $('#operator').val();
var fieldvalue = $('#searchstring').val();
var filter=searchfield+operator+"'"+fieldvalue+"'";
console.log(filter);
$.get('SearchData', {
filter : filter
}, function(responseText) {
$('#example').html(responseText);
console.log('dddd');
});
});
});
</script>
这是 servlet 代码:
String query = "select * from searchsample";
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
renderingengine = resultSet.getString("RenderingEngine");
browser = resultSet.getString("Browser");
platform = resultSet.getString("platform");
engineversion = resultSet.getString("EngineVersion");
cssgrade = resultSet.getString("CSSGrade");
}
Gson gson = new Gson();
JsonVo jsondata=new JsonVo();
jsondata.setRenderingengine(renderingengine);
jsondata.setBrowser(browser);
jsondata.setPlatform(platform);
jsondata.setEngineversion(engineversion);
jsondata.setCssgrade(cssgrade);
String toJson = gson.toJson(jsondata);
System.out.println("Json Format : " +toJson);
request.setCharacterEncoding("utf8");
response.setContentType("application/json");
我想你只需要写下一行就可以将 JSON
从 servlet
发送到 JSP
。
response.setContentType("application/json");
response.getWriter().write(toJson);
我在这里通过 AJAX 从 JSP.In servlet 调用调用 servlet,从数据库中获取数据并将这些数据转换为 JSON 使用我想要的 Gson.After 格式将此 json 对象发送到此 JSP.But 此处我无法发送 json object.Please 建议我? AJAX 调用的代码:
<script type="text/javascript">
$(document).ready(function() {
$('#subbtn').click(function(event) {
console.log("Hello");
var searchfield = $('#searchfields').val();
var operator = $('#operator').val();
var fieldvalue = $('#searchstring').val();
var filter=searchfield+operator+"'"+fieldvalue+"'";
console.log(filter);
$.get('SearchData', {
filter : filter
}, function(responseText) {
$('#example').html(responseText);
console.log('dddd');
});
});
});
</script>
这是 servlet 代码:
String query = "select * from searchsample";
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
renderingengine = resultSet.getString("RenderingEngine");
browser = resultSet.getString("Browser");
platform = resultSet.getString("platform");
engineversion = resultSet.getString("EngineVersion");
cssgrade = resultSet.getString("CSSGrade");
}
Gson gson = new Gson();
JsonVo jsondata=new JsonVo();
jsondata.setRenderingengine(renderingengine);
jsondata.setBrowser(browser);
jsondata.setPlatform(platform);
jsondata.setEngineversion(engineversion);
jsondata.setCssgrade(cssgrade);
String toJson = gson.toJson(jsondata);
System.out.println("Json Format : " +toJson);
request.setCharacterEncoding("utf8");
response.setContentType("application/json");
我想你只需要写下一行就可以将 JSON
从 servlet
发送到 JSP
。
response.setContentType("application/json");
response.getWriter().write(toJson);