Foros de discusión

RE: problem in Ajax call

Koen Cleynhens, modificado hace 13 años.

problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
Hello I developed a portlet to run my current J2EE web app.

In this web app there is included a function to refresh data with Ajax call.

And the point is, that it is working on the first request, but a second request does not worked anymore.

Can please someone look at this problem?

I have to do the tricky thing with exclusive, because other wise my answer for the first request was contain a lot of other data...

The code is:

<script type="text/javascript">
//<![CDATA[

var xmlHttp;

function init() {
}

function refreshCarMakes() {
xmlHttp = null;
if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e1) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e2) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e3) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e4) {}}}}}

if (xmlHttp != null) {
<% PortletURL actionURL = renderResponse.createActionURL();
actionURL.setWindowState(LiferayWindowState.EXCLUSIVE);
%>
var url = "<%=actionURL%>";

xmlHttp.onreadystatechange = loadCarMakes; xmlHttp.open("POST", url, true); xmlHttp.send(null);
}
}


function loadCarMakes() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

var makes = xmlHttp.responseText.split("\n");


for (i = 1 ; i < makes.length ; i++) {
if (makes != "" && makes != "carTypesList_") {
var newOption = document.createElement('option');
newOption.value = makes;
newOption.text = makes;

try {
carMakeSelect.add(newOption, null);
} catch (ex) {
carMakeSelect.add(newOption);
}
}
else if (makes == "carTypesList_")
break;
}
}
}



//]]>
</script>
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
Hello Koen,

it seems very complex. Is it really the best way?

1, Why didn't you use the jQuery or some other framework for the ajax calls? You don't need to write it from scratch and it works out of the box.
2, Why do you use the ActionURL instead of ResourceURL?

-- tom
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
@Tomas

1. it is my intention to test it with the standard way of working in Liferay. But It is an application that is working now standalone, so my fist approach is/was to modify as least as possible...
=> are there some simple example to start from?

2. Good question: I guess I toke the ActionURL because for the other request I use the <form action="<portlet:actionURL... approach.
=> What is the difference?
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
ad 1, I use the jQuery (which is bundled with Liferay), you can bundle it in your standalone application too. http://api.jquery.com/jQuery.post/

I've added an example, it is just an idea how to implement it, I haven't try to compile it or run emoticon


ad 2, Serving resources mechanism is for sending html snippets (just serveResource method is invoked), action phase is for changing the portlet state (processAction and doView/Edit/Help are invoked). For more info see the JSR-286 specification.


Portlet JSP Example:
<script type="text/javascript">
    function  <portlet:namespace />refreshCarMakes(){
        // url to the resource myCars
        var carsUrl = '<portlet:resourceURL id="myCars" />';

        // send AJAX call using POST
        jQuery.post(carsUrl, function(data) {
            // create new content from the data
            var buffer = "";
            for(i = 0; i < data.cars.length; i++){
                var car = data.cars[ i ];
                buffer += "<option value="+car.id+">";
                buffer += car.name + " wheels: " + car.wheels;
                buffer += "</option>"; 
            }
            // replace content
            jQuery("<portlet:namespace />selectedCarOption").html(buffer);
        }, "json");
    }
</script>

<input type="button" value="Load cars" onclick="<portlet:namespace />refreshCarMakes()">
<select name="selectedCar" id="<portlet:namespace />selectedCarOption"></select>


Portlet Example:
public class MyPortlet extends GenericPortlet {
...   
    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
       
        response.setContentType("application/json");
        String resourceId = request.getResourceID();
        
        if (resourceId.equals("myCars")) {
            PrintWriter writer = response.getWriter();
            writer.println("{");
            writer.println("  'cars': [");
            writer.println("    {'id':1,'name':'myCar','wheels'=4},");
            writer.println("    {'id':2,'name':'myCar','wheels'=3},");
            writer.println("    {'id':3,'name':'myCar','wheels'=2},");
            writer.println("    {'id':4,'name':'myCar','wheels'=1}");
            writer.println("  ]");
            writer.println("}");
        }
    }
...
}


-- tom
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
@Tomas

Thanks for the info.

I'm already one step further. I guess the solution will be in the fact to use resourceUrl.

But the problem now is that in my custom portlet I have some stuff that I need in the doView method.

So now I'm looking to have a way of working to have a call in the next method:

public void serveResource(
ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws IOException, PortletException {

doView((RenderRequest)resourceRequest, (RenderResponse)resourceResponse);

}

=> But of course this will give some casting exception. Is there a way that I can just call the doView method from inside the serveResource method?
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
@Tomas:

Just to say,forget previous reply.

I just refactored my doView method so that I call another method with the PortletRequest and PortletResponse object.

No I can call this method from my serveResource method..


Thanks a lot for your help.

Maybe you know also what the problem can be for another problem for me.

I make in a action a dynamic pdf with FOP , and open it in another window. But the browser shows only the pdf code itself and not with acrobat or so...
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
Koen,

do you set correct mime type in the response?

For example:
response.setContentType("application/pdf");


How do you generate your PDF, using serve resource or servlet?

-- tom
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
@Tomas:

Other point about Ajax. I just changed my portlet behaviour. The page with Ajax call's was first the first page in the portlet. Now I put another action before this action/page.

The problem now is that the resource URL is contain the parameters from the action handling before...

Can I do a kind of clean on the resource URL?
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
Hi,


The page with Ajax call's was first the first page in the portlet. Now I put another action before this action/page.

I am not sure I understand it.

The problem now is that the resource URL is contain the parameters from the action handling before...


Maybe try to look at the ticket LPS-3022. Is it the same behaviour as yours?

-- tom
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
YEp, the same behaviour.

But what now?

Is there a work around without modify the code?
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
If I understand it well, you should set url.setCopyCurrentRenderParameters(false) before rendering the URL.

Try something like this, it could work:

<portlet:resourceurl id="myResource" var="myResourceURL">
   <portlet:param name="myParam" value="myValue" />
   ...
</portlet:resourceurl>
&lt;% 
  myResourceURL.setCopyCurrentRenderParameters(false);
  out.println(myResourceURL.toString()); 
%&gt;


-- tom
Koen Cleynhens, modificado hace 13 años.

RE: problem in Ajax call

Junior Member Mensajes: 83 Fecha de incorporación: 23/02/10 Mensajes recientes
Thanks,

it works now.

I just add next lines in my JSP:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

I know it is not the most clean way, but I don't know another solution for the moment...
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
OK, good to know emoticon

I'll append it as a workround to the LPS-3022

-- tom
thumbnail
Muhammad Asif, modificado hace 12 años.

RE: problem in Ajax call

Junior Member Mensajes: 25 Fecha de incorporación: 11/04/11 Mensajes recientes
Koen Cleynhens:
Thanks,

it works now.

I just add next lines in my JSP:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

I know it is not the most clean way, but I don't know another solution for the moment...



I think this does not work in 6.0.6 portal and 6.0.5 plugin sdk versions. I could not fine PortletURLImpl here.

Regards,
Asif
thumbnail
Mani kandan, modificado hace 13 años.

RE: problem in Ajax call

Expert Mensajes: 492 Fecha de incorporación: 15/09/10 Mensajes recientes
Tomas Polesovsky:
If I understand it well, you should set url.setCopyCurrentRenderParameters(false) before rendering the URL.

Try something like this, it could work:

<portlet:resourceurl id="myResource" var="myResourceURL">
   <portlet:param name="myParam" value="myValue" />
   ...
</portlet:resourceurl>
&lt;% 
  myResourceURL.setCopyCurrentRenderParameters(false);
  out.println(myResourceURL.toString()); 
%&gt;


-- tom

I am also getting the same problem
I tried what you mentioned but I could solve
Throwing error : The method setCopyCurrentRenderParameters(boolean) is undefined for the type String....
any other solution?
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
Mani,

that was my fault, var="myResourceURL" creates String object, not ResourceURL object. You have to use it the way Koen mentioned:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>


-- tom
thumbnail
Mani kandan, modificado hace 13 años.

RE: problem in Ajax call

Expert Mensajes: 492 Fecha de incorporación: 15/09/10 Mensajes recientes
<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

Hi Tom,

I tried the above coding in my jsp page but its throwing this error after click the action:

ERROR [PortletRequestDispatcherImpl:107] org.apache.jasper.JasperException: An exception occurred processing JSP page
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
Hi,

the error is not root cause. It just says that there was an error in JSP. Do you have root cause?

-- tom
thumbnail
Mani kandan, modificado hace 13 años.

RE: problem in Ajax call

Expert Mensajes: 492 Fecha de incorporación: 15/09/10 Mensajes recientes
Tomas Polesovsky:
Hi,

the error is not root cause. It just says that there was an error in JSP. Do you have root cause?

-- tom


Hi tom,

root cause means? what you are mentioning?
thumbnail
Tomas Polesovsky, modificado hace 13 años.

RE: problem in Ajax call

Liferay Master Mensajes: 676 Fecha de incorporación: 13/02/09 Mensajes recientes
I mean - the JSP page cannot be processed. But you haven't posted why - what is the cause why the page cannot be processed emoticon

-- tom
thumbnail
Victor Zorin, modificado hace 13 años.

RE: problem in Ajax call

Liferay Legend Mensajes: 1228 Fecha de incorporación: 14/04/08 Mensajes recientes
Browser caching may prevent your ajax calls going back to your portal, you may check the other thread too:
http://www.liferay.com/community/forums/-/message_boards/message/4381676