Click here to Skip to main content
14,876,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I've developed a simple portlet using ajax.But that is not working. here I'm posting my source code, pls help me to sort out the problem

================view.jsp===============
HTML
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.Validator" %>
<%@ page import="javax.portlet.PortletPreferences" %>

This is the Ajax Portlet portlet in View mode.

<portlet:defineobjects xmlns:portlet="#unknown" />


<portlet:resourceurl id="ajaxCall" var="ajaxCallURL" xmlns:portlet="#unknown"></portlet:resourceurl>
<html>
<head>


<script type="text/javascript">




function callAjax(){
		var name=$("#name").val();
		var addresss= $("#addr").val();
		document.write("Name="+name+"  address="+addresss);
			var url="<%=ajaxCallURL%>";
			document.write(url);
			 $.ajax({
				
				url:url,
				type:'GET',
				data:{
					nKey:name,
					aKey:addresss		
					
				},
				datatype:'json',
		        success: function(data){
		            var obj = $.parseJSON(data);
		            alert(obj);
		        },

			error:function(){
				alert("problem!!");
			}
		    }); 
			document.write("end");
			
		}
	

 
</script>

</head>

	<aui:form method="post" id="<portlet:namespace/>fm" name="<portlet:namespace/>fm" xmlns:aui="#unknown">
			
			Name   :<aui:input type="text" id="name" name="" /><br />
			Adress :<aui:input type="text" id="addr" name="" /><br />
			<aui:button type="button" value="submit" önclick="callAjax()" />
	
	</aui:form>

</html>

<========AjaxPortlet================

import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

public class AjaxPortlet extends MVCPortlet {
	
	    
    
    @Override
    public void serveResource(ResourceRequest resourceRequest,
    		ResourceResponse resourceResponse) throws IOException,
    		PortletException {
    		
            JSONObject json = JSONFactoryUtil.createJSONObject();
            try{                
            
            String name=resourceRequest.getParameter("nKey");
            String address=resourceRequest.getParameter("aKey");
            System.out.println("User name="+name+"\t\tAddress="+address);
            
            resourceResponse.setCharacterEncoding("UTF-8");
            json.put("name",name);
             json.put("address",address);
             System.out.println("values"+json.toString());
             resourceResponse.getWriter().write(json.toString());
            }catch (Exception e) {
        }        
    	
    }
    
    
    }


Thank u
Posted
Updated 8-May-14 0:07am
v2
Comments
Sunasara Imdadhusen 8-May-14 6:07am
   
Not working means what? is it giving you an error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900