掲示板
Three questions regarding JS, Ajax and resourceURL in Liferay
6年前 に Pablo Yuste Soto によって更新されました。
Three questions regarding JS, Ajax and resourceURL in Liferay
New Member 投稿: 14 参加年月日: 17/10/03 最新の投稿
Hi!
I'm performing an Ajax operation that loads an image in base64 and paints it in a div. After struggling with the path thingy and a few more things, it began to work, but I'm having a problem, aswell some questions about what's going on here. I think they aren't hard questions, though I don't find any info about them:
1. (Problem): I load the image and it gets shown, which is great, but... the page automatically reloads after that. How can I prevent that?
2. (Question): Can I use ES6 in Liferay's JS? Promises, "let", .map(), etc, in AUI() methods?
3. (Question): What's the correct way of working with paths? Currently, I'm providing the full path to the image, but I would want something more dynamic, something that lets me know the portlet's root path. I'll be ok with that.
Also, this is my code, in case you wondered:
The JS:
The serveResource method:
Thank you!
I'm performing an Ajax operation that loads an image in base64 and paints it in a div. After struggling with the path thingy and a few more things, it began to work, but I'm having a problem, aswell some questions about what's going on here. I think they aren't hard questions, though I don't find any info about them:
1. (Problem): I load the image and it gets shown, which is great, but... the page automatically reloads after that. How can I prevent that?
2. (Question): Can I use ES6 in Liferay's JS? Promises, "let", .map(), etc, in AUI() methods?
3. (Question): What's the correct way of working with paths? Currently, I'm providing the full path to the image, but I would want something more dynamic, something that lets me know the portlet's root path. I'll be ok with that.
Also, this is my code, in case you wondered:
The JS:
function ajaxCall(){
AUI().use('aui-io-request', function(A){
A.io.request('${imagen}', {
method: 'post',
on: {
success: function() {
console.log("Respuesta: ", this.get('responseData'));
var bytes = this.get('responseData');
var img = getImage(bytes);
document.getElementById("imageTest")
.append(img);
}
}
});
});
}
The serveResource method:
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
try {
resourceResponse.setContentType("image/jpeg");
PrintWriter out = resourceResponse.getWriter();
BufferedInputStream input = new BufferedInputStream(new FileInputStream("C:\\Eclipse\\com.liferay.portal.plugins.sdk-1.0.11-withdependencies\\portlets\\PortletConsumidor-portlet\\docroot\\html\\portletconsumidor\\image.jpg"));
int b = 0;
while((b = input.read()) != -1){
out.write(b);
}
out.flush();
super.serveResource(resourceRequest, resourceResponse);
}catch(IOException e) {
System.out.println("ERROR: "+e.getMessage());
}
}
Thank you!