[英]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在問題編輯中回答。轉換為社區Wiki答案。請參閱無答案的問題,但問題已在評論中解決(或擴展為聊天) )
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();
}
}
這是阿賈克斯
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);
}
});
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.