Reputation: 365
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
Upvotes: 1
Views: 3023
Reputation: 236
You can generate resourceURL using java script
var resourceURL = Liferay.PortletURL.createResourceURL();
resourceURL.setPortletId(86);
resourceURL .setParameter("key2", "value");
resourceURL.toString();
Upvotes: 1
Reputation: 365
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.
Upvotes: 0
Reputation: 3077
I can think of two options for you.
To use JSP tags and EL in a JavaScript file, you will need to serve it from a JSP. I've done this in portlets before by using the serveResource
method to serve the JavaScript file. This way you can use the same portlet tags in your JavaScript JSP.
In your portlet JSP
<portlet:resourceURL var='portletJS' id='javascriptResourceID' />
<script type='text/javascript' src='${portletJS'></script>
In your portlet's serveResource
method.
if (request.getResourceID == "javascriptResourceID) {
//render JSP for JavaScript
}
The other option is to set a JavaScript variable in your portlet and then use that variable in your static JavaScript file.
In your portlet JSP
<script type='text/javascript'>
var portletAjaxURL = '<portlet:resourceURL id="ajax" escapeXml="false" />';
</script>
In your static JavaScript file
$.ajax({
method : "POST",
url : portletAjaxURL,
data : {
...
Upvotes: 0