处理ajax和java servlet之间的请求和响应
Handle request and response between ajax and java servlet
我试图处理ajax和servlet之间的请求/响应:用户单击一个Google地图标记,并通过ajax调用使用他的id的相对于标记的评论。
这应该是Ajax代码
function infoCallback(infowindow, marker) {
return function() {
$.ajax({
type: 'POST',
url: 'commentListener',
data: {
id: marker.id,
comment:$('#comment').val()
},
success:function(data){
var comment = $('#output').html(data);
infowindow.setContent(comment);
infowindow.open(map, marker);
}
});
};
}
这应该是Servlet代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long id = Long.parseLong(request.getParameter("id"));
String comment = //comment relative to the id
/*Way to send the comment to the infowindow*/
response.getWriter().write("{comment:"+comment+"}");
}
对不起,如果这一切都不是很清楚!
(OP在问题编辑中回答)转换成社区维基的答案。参见没有答案的问题,但问题已在评论中解决(或在聊天中扩展))
OP写:
使用PrintWriter
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long id = Long.parseLong(request.getParameter("id"));
String comment = //get comment by id
try {
PrintWriter out;
out = response.getWriter();
out.print(comment);
} catch (IOException e) {
e.printStackTrace();
}
}
这是ajax
function infoCallback(infowindow, marker) {
return function() {
$.ajax({
type: 'POST',
url: 'commentListener',
data: {
id: marker.id,
},
success:function(result){
infowindow.setContent(result);
infowindow.open(map, marker);
}
});
};
}
相关文章:
- ajax请求的顺序总是不同的
- Meteor如何接收HTTP请求
- 有没有一种方法可以防止img get请求使用css或js发生
- 从ajax请求中获取javascript对象
- JSONP请求返回结果,但也触发error_callback
- 在localhost Dev Box上测试JSONP请求的最佳方式
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 在openshift node js应用程序中获取请求
- 反应路由器弄乱了请求网址
- 在我的情况下,如何进行http请求
- 使用密码对话框Javascript请求帮助
- servlet中的请求对象,而不是从jsp接收参数值
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- JavaScript代码未正确检查ajax请求
- node.js请求数据事件未在CORS ajax调用中触发
- 用户请求和AJAX/资源请求之间的差异
- 如何发送带有get参数的ajax请求和post请求
- 区分HTTP请求和Ajax请求