D3GAN
D3GAN

Reputation: 640

Using ajax in liferay

I have a portlet with a button in it. when this button clicked I send an ajax request to one of my controller in my project and get back result in normal case I did it like this:

web.xml:

<servlet>
    <servlet-name>ctrl</servlet-name>
    <servlet-class>controller.Translator_ctrl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ctrl</servlet-name>
    <url-pattern>/translate</url-pattern>
</servlet-mapping>

view.jsp:

function send_form(){
            var xhr=new XMLHttpRequest();
            var base;
            var from=document.getElementById("from").value;
            var to=document.getElementById("to").value;
            var lang_from=document.getElementById("lang_from").value;
            var lang_to=document.getElementById("lang_to").value;
            //alert(":D:D:D"+from+"::::"+to+":::"+lang_from+":::"+lang_to);
            xhr.onreadystatechange=function (){
                if(xhr.readyState==4){
                    base=xhr.responseText;
                    //document.getElementById("to").value=base;
                    alert(base);
                }
            }
            xhr.open("GET","translate?mode=ajax&from="+from+"&to="+to+"&lang_from="+lang_from+"&lang_to="+lang_to,true);
            xhr.send();
        }

Now I do not know how to do it in Liferay? How can I do this in Liferay?

Upvotes: 1

Views: 587

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

utilize the "resource" lifecycle, which is there for exactly this purpose. You'll send the request to and implement the serveResource method in your portlet (using whatever implementation/framework you use). The code that goes it there is basically the same as you had in the servlet when you didn't use a portal environment

Upvotes: 1

Related Questions