7/24/2012

AlloyUI: How to send an AJAX request

Just a short instruction on how to send an AJAX - request with AlloyUI. First you need to create an MVCPortlet so that you can fire to distinct methods.


Create an action method like this:

public void somethingHappensHere(
  ActionRequest actionRequest, 
  ActionResponse actionResponse) throws IOException, PortletException {
}
In your AlloyUI page add the following to a button of your choice. The variable "myActionURL" should point to your "somethingHappensHere" method in your portlet.
var url = '<%= myActionURL.toString() %>';
A.io.request(url,{
//this is the data that you are sending to the action method
data: {
   <portlet:namespace />id: selected,
   <portlet:namespace />name: name,
},
dataType: 'json',
on: {
  failure: function() { //do something },
  success: function(event, id, obj) { //do something else}
}
});



If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.