POST Servlet using Page resource type - Unable to pass form data

Avatar

Avatar

santhoshsrg

Avatar

santhoshsrg

santhoshsrg

14-05-2020

Hi everyone,

 

I wanted to create a POST servlet using Resource Type and pass form data. I will be calling this servlet from Component X which is an embedded component. I created a POST servlet using Page component's resourceType. It works fine when form data is not passed. But when form data is passed, it is throwing some exceptions like in the screenshot below. The data i am trying to pass is "img" as in screenshot.

 

I also tried to create POST servlet using Component Y's resourceType and i was able to pass form data here. Since I am using an embedded component, Servlet using Page resource type will be apt for this.

 

Any help would be great. 

 

Screenshot 2020-05-14 at 7.31.47 PM.pngScreenshot 2020-05-14 at 7.31.02 PM.png

View Entire Topic

Avatar

Avatar

santhoshsrg

Avatar

santhoshsrg

santhoshsrg

15-05-2020

Hi,

 

I am passing Image in base64 format to servlet as form data and in servlet i am uploading image to asset. The issue occurs before servlet code ran. 

 

JS Code:

let data = {img: img};
$.ajax({
type: "POST",
url: url,
data,
success: function (response) {
displayModal(SUCCESS_MSG);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
    displayModal(ERROR_MSG);
}
}).done(function() {
    $(".logo-mobile img").show();
});

 

Servlet code: 

@component(immediate = true, service = Servlet.class,

property={

        "sling.servlet.methods=" + HttpConstants.METHOD_POST,

        "sling.servlet.resourceTypes="+ "project/components/structure/page",

        "sling.servlet.selectors=" + "save",

        "sling.servlet.extensions=" + "html"

})

public class SaveScreenshotServlet extends SlingAllMethodsServlet  {

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

throws ServletException, IOException {

    String img = request.getParameter(IMG_PARAM);

}