Friday, March 7, 2014

Ajax call using jQuery in Liferay Spring Portlet

1. JSP Code


<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<liferay-portlet:resourceURL var="sampleUrl" />
<input type="button" value="Ajax Call" onclick="ajaxMethod('${sampleUrl}');" />
<script>
function ajaxMethod(sampleUrl){
$.ajax({
type: 'GET',
url: sampleUrl,
cache:false,
async:true,
error: function(){
alert('error');
},
dataType : "html",
data :{
name:"hello123"
},
success: function(htmlData) {
alert('response : ' + htmlData);
},
complete: function(){
}
});
}
</script>
view raw jquery-ajax-jsp hosted with ❤ by GitHub
2. Method code in controller class


@ResourceMapping
public void serveResource(ResourceRequest request, ResourceResponse response)
{
System.out.println("method called");
try {
response.getWriter().write("<h1>Hello</h1>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

1 comment:

  1. Hi!
    Also you can receive data in serveResource?
    Can I receive name:"hello123" ???

    I have tried : String name = ParamUtil.getString(request, "name");

    But is not working ...
    Any suggestions?
    Thanks!

    ReplyDelete