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();
}
}
}