Search code examples
jqueryajaxliferayportletspring-portlet-mvc

liferay resource within included javascript file


when i use this :

<portlet:resourceURL var="ajaxURL" id="ajax" escapeXml="false" />


 $.ajax({
    method : "POST",
        url : "${ajaxURL}",
        data : {
    ...

inside my ajax call that is not included in page it works.

but when i try to include that same javscript code on page i get error:

WARN  [http-bio-8080-exec-27][404_jsp:109] /$%7BajaxURL%7D

My question is how to pass that resource url to included javascript or how to generate it within javascript.

i have tried: var urlVar = '<portlet:resourceURL var="ajaxURL" id="ajax" escapeXml="false" />'

and when i use urlVar on this way:

$.ajax({
method : "POST",
    url : urlVar,
    data : {
...

It doesnt works. Does anyone knows how to solve this issue? Thanks


Solution

  • I have found the solution. One way doing this is to define resource url on jsp page for example like this:

    <portlet:resourceURL var="yourResourceURL" id="yourResource" />
    

    and to pass it from jsp within paramter to the function that is located in external js file.

    for example in js:

    function test(urlVar) {
        alert(urlVar);
            $.ajax({
                method : "POST",
                url : urlVar,
                success : function(data) {
    
                }
            });
    }
    

    and in jsp call that function like this:

     test(" ${yourResourceURL} ");
    

    and it should work.