我正在使用XMLHttpRequest使用javascript发送ajax请求,但参数
发送到servlet的“_ MNAME = ABC&安培;代码= 1”
不会存储在fileItems上,即
List<FileItem> fileItems = servletFileUpload.parseRequest(request);
当我遍历fileItems并打印它时,没有打印出来。 我正在使用multipart / form-data发送它,因为我稍后会添加fileupload。 但目前只发送参数(表单参数这只是一个片段) 我怎么能让它接受我发送的参数,然后在servlet上接收它 我非常想使用这个servlet而不想创建其他的servlet (虽然欢迎提出建议)
当我发送正常的多部分请求时,servlet正常运行没有任何问题,但在ajax请求的情况下不一样
我无法设置
requestObject.setRequestHeader( “内容长度”,param.length); requestObject.setRequestHeader( “连接”, “关闭”);
它给我错误405不安全的操作是什么原因,是否有必要发送ajax请求? 我还想使用multipart / form-data发送请求。 我是否正确发送了ajax请求? servlet有什么问题吗? 我怎样才能读取servlet中的有效负载? 请指导我可能错在哪里?
这是代码
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();
}
}
}