0

i'm sending an ajax request using javascript using XMLHttpRequest but the parameters

"_mname=abc&code=1"

sent to the servlet does not getstored on fileItems ie

List<FileItem> fileItems = servletFileUpload.parseRequest(request);

when i iterate over fileItems and print it nothing is printed. i'm sending it using multipart/form-data because i will add fileupload later. but currently only sending parameters(form parameters this is only a snippet) how can i make it to accept the parameters that i send and then recieve it on the servlet i strictly want to use this servlet and don't want create other one (although suggestions are welcome)

when i send a normal multipart request the servlet runs properly without any problems but not the same in case of ajax request

i'm unable to set

requestObject.setRequestHeader("Content-length",param.length); requestObject.setRequestHeader("connection","close");

it gives me error 405 unsafe operation what is the reason and is it necessary while sending ajax request? i also want to send request using multipart/form-data. is ajax request sent correctly by me? is there any problem with the servlet? how can i read the payload in the servlet? please guide where i might be possibly wrong?

here's the code

    var requestObject = null;
    function createRequestObject(){
        try{
            requestObject = new XMLHttpRequest();
        }
        catch(e){

            try{
                requestObject = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e){
                try{
                    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    alert("update your browser");
                }
            }
        }
        return requestObject;
    }

    function sendAjaxRequest(){
        var requestObject = createRequestObject();
        if(requestObject){
            requestObject.open("POST","personaldetails.do",true);
            var boundary = "----rrrrrr----";
            requestObject.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + boundary);
            //requestObject.setRequestHeader("Content-length",param.length);    //when this line is opened it gives 405 error refused unsafe operation what is the reason form the error?
            //requestObject.setRequestHeader("connection","close");     //when this line is opened it gives 405 error refused unsafe operation what is the reason form the error?
            requestObject.onreadystatechange = sendRequest;
            requestObject.send("_mname=abc&code=1");
        }
    }
    function sendRequest(){
        if(requestObject.readyState == 4 && requestObject.status == 200){
            if(requestObject.responseText){
                console.log("update successfull");
            }
        }
    }


    public class PersonalDetailsServlet extends HttpServlet{
        public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{

        if(ServletFileUpload.isMultipartContent(request)){
            System.out.println("request is multipart");

            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

            try{
                List<FileItem> fileItems = servletFileUpload.parseRequest(request);
                System.out.println(pds+"fileItems :"+fileItems);

                for(FileItem fileItem : fileItems){
                    System.out.println("fieldname :"+fileItem.getFieldName());
                    System.out.println("inside for loop");
                    field = fileItem.getFieldName();
                    if(fileItem.isFormField()){
                         if(field.equals("_mname")){
                            motherName = fileItem.getString().trim();
                            System.out.println(pds+"mother name :\t\t>"+motherName+"<");

                        }else if(field.equals("code")){
                            code = fileItem.getString().trim();
                            System.out.println(pds+"code :\t\t>"+code+"<");

                            if(code.equals("1")){
                                flag = true;
                                System.out.println("ajax request recieved");
                            }
                        }

                    }else {
                        System.out.println("file for upload recieved");
                    }
                }//for loop end
            }catch(FileUploadException e){
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
prabal
  • 1
  • 1
  • the question is entirely different from the marked link @balusC I am able to successfully receive parameters using a normal request but not so in case of ajax request .The parameters received is not showing up on fileItems.It is also likely to notice that i'm not uploading any file but only sending form parameters(not using form data). – prabal Aug 29 '15 at 12:25
  • What part exactly of "That's only possible with XHR FormData API" didn't you understand? How exactly did the example in the answer fail for you when sending `multipart/form-data` request using FormData API? Whether you send a file along or not is irrelevant. – BalusC Aug 29 '15 at 13:41
  • okay let me rephrase why am i not getting fileItem parameter values data on server side when i send an ajax request even though in payload parameters are shown.I am able to properly receive parameters when i send data using normal multipart request.So am I sending ajax request "incorrectly"? please have glance over my code.would really appreciate your help – prabal Sep 02 '15 at 18:28
  • You're not using XHR FormData API. That's the problem. – BalusC Sep 02 '15 at 18:33

0 Answers0