留言板
Ajax call without refereshing page.
jsp page>>>>>>>
<portlet:resourceURL id="deletePost" var="deletePostUrl">
<portlet:param name="postId" value="<%=String.valueOf(s.getPostId()) %>"/>
</portlet:resourceURL>
<a class="deleteSocialPost" href="<%=deletePostUrl%>">Delete</a>
<script type="text/javascript">
$(document).ready(function()
{
$('.deleteSocialPost').click(function(event) {
var $this = $(this);
event.preventDefault();
var deletePostUrl = $this.attr('href');
//Make ajax call
$.ajax({
type : "POST",
url : deletePostUrl,
data:{},
cache:false,
dataType: "Json",
success : function(data)
{
//var jsonDeletePost = data['postID'];
//$this.next().html(jsonDeletePost);
$this.parent().parent().prev().remove();
$this.parent().parent().remove();
},
error : function(XMLHttpRequest, textStatus, errorThrown)
{
}
});
});
});
</script>
Server Side >>>>>>>>
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) {
if(resourceRequest.getResourceID().equals("deletePost")){
long postId = ParamUtil.getLong(resourceRequest, "postId");
try
{
JSONObject jsonDeletePost=JSONFactoryUtil.createJSONObject();
SocialPostLocalServiceUtil.deleteSocialPost(postId);
jsonDeletePost.put("postID", postId);
resourceResponse.getWriter().write(jsonDeletePost.toString());
}
catch(PortalException e)
{
e.printStackTrace();
}
}
<portlet:resourceURL id="deletePost" var="deletePostUrl">
<portlet:param name="postId" value="<%=String.valueOf(s.getPostId()) %>"/>
</portlet:resourceURL>
<a class="deleteSocialPost" href="<%=deletePostUrl%>">Delete</a>
<script type="text/javascript">
$(document).ready(function()
{
$('.deleteSocialPost').click(function(event) {
var $this = $(this);
event.preventDefault();
var deletePostUrl = $this.attr('href');
//Make ajax call
$.ajax({
type : "POST",
url : deletePostUrl,
data:{},
cache:false,
dataType: "Json",
success : function(data)
{
//var jsonDeletePost = data['postID'];
//$this.next().html(jsonDeletePost);
$this.parent().parent().prev().remove();
$this.parent().parent().remove();
},
error : function(XMLHttpRequest, textStatus, errorThrown)
{
}
});
});
});
</script>
Server Side >>>>>>>>
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) {
if(resourceRequest.getResourceID().equals("deletePost")){
long postId = ParamUtil.getLong(resourceRequest, "postId");
try
{
JSONObject jsonDeletePost=JSONFactoryUtil.createJSONObject();
SocialPostLocalServiceUtil.deleteSocialPost(postId);
jsonDeletePost.put("postID", postId);
resourceResponse.getWriter().write(jsonDeletePost.toString());
}
catch(PortalException e)
{
e.printStackTrace();
}
}
David H Nebinger,修改在9 年前。
RE: Ajax call without refereshing page.
Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
Is there a question here?
Or do you want a critique, such as a) you shouldn't be using jQuery, or b) Liferay has it's own "ready" entry points that you should use instead of document.ready(), etc.?
Or do you want a critique, such as a) you shouldn't be using jQuery, or b) Liferay has it's own "ready" entry points that you should use instead of document.ready(), etc.?