How to get the file name and other requested parameter to use the Resource method with ajax call?

I use ajax to send my jsp page data without refreshing the page in the liferay user portlet.

I ran into two problems:

  • When I call the Ajax function for the resource server method, it gets all the ajax data in this method, but when everything is done, when this is successful, I can not get any data in return. So, how can I get some data in response after the successful implementation of the Ajax function.

  • I used a simple control to upload HTML files in my form, and when I use a simple submit method, I call a upload request to get the entire file attribute and also upload the file with the upload request, but in case of Ajax I have to do everything with resource actions in my resource server method in the action class and I get all null values ​​with a load request, so how can I solve this problem?

I provide my one example Ajax call and my resource server method

This is my simple ajax call on jsp page

 <script type="text/javascript"> function addToDo(addToDo) { var todo = document.getElementById('toDo').value; alert(""); var adv_name = document.getElementById('advertise_name').value; var keywords = document.getElementById('keywords').value; var adv_url = document.getElementById('adv_url').value; var fileUpload = document.getElementById('fileUpload').value; var mediatype= $('#mediatype option:selected').val(); $.ajax({ url :addToDo, data: { "todo":todo, "adv_name":adv_name, "mediatype":mediatype, "keywords":keywords, "adv_url":adv_url, "fileUpload":fileUpload, "CMD":"addToDo" }, type: "GET", timeout: 20000, dataType: "text", success: function(data) { alert(data); //when i alert this data..its always null } }); } </script> 

Below is my download control:

 <label>Browse</label> <input name="fileUpload" id="fileUpload" type="file" /> <span id="restError2" style="color: #C62626;" class="help-block"></span> 

on one button, I call the ajax method above and which is redirected to the resorce server method as follows:

 @Override public void serveResource(ResourceRequest request, ResourceResponse response){ if(request.getParameter("CMD").equals("addToDo")) { System.out.println("came here for add"); // here am converting this resource request to uploadportletrequest because i have enctype="multipart/form-data" UploadPortletRequest uploadReq = PortalUtil.getUploadPortletRequest(request); sourceFileName = uploadReq.getFileName("fileUpload");// uploaded // filename advertise_name = uploadReq.getParameter("advertise_name"); //this value getting null adv_link = uploadReq.getParameter("adv_url"); //this value getting null keyword = uploadReq.getParameter("keywords"); //this value getting null String resourceID = request.getResourceID(); System.out.println(resourceID); } } 
0
source share
1 answer

For the first question, see this SO answer .

In the second question, I would say that you are missing a prefix for your parameters.

try changing your ajax call (I only change the fields that you specify in the serveResource method that gets null)

  $.ajax({ url :addToDo, data: { "todo":todo, "<portlet:namespace />adv_name":adv_name, "mediatype":mediatype, "<portlet:namespace />keywords":keywords, "<portlet:namespace />adv_url":adv_url, "fileUpload":fileUpload, "CMD":"addToDo" }, type: "GET", timeout: 20000, dataType: "text", success: function(data) { alert(data); //when i alert this data..its always null } }); 

and necessarily

 <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %> 

at the top of your jsp.

Also note that in your serveResource method you have

 advertise_name = uploadReq.getParameter("advertise_name"); 

but "adv_url":adv_url, sending "adv_url":adv_url, pay attention to the names.

One more note: you use GET instead of POST.

Another thing to keep in mind is that downloading files using ajax is not supported in all browsers. For a list of supported browsers, see this .

+1
source

Source: https://habr.com/ru/post/1444280/


All Articles