Java 为什么my formdata.append未将键值对发送到服务器?
我认为它应该是非常简单的jquery代码,但我看不到它将任何结果传递给服务器。我现在只想测试字符串,不需要文件!在服务器中,我只在请求中看到多部分/表单数据,但参数字段是{}。。。。。请帮忙Java 为什么my formdata.append未将键值对发送到服务器?,java,javascript,jquery,apache-commons-fileupload,Java,Javascript,Jquery,Apache Commons Fileupload,我认为它应该是非常简单的jquery代码,但我看不到它将任何结果传递给服务器。我现在只想测试字符串,不需要文件!在服务器中,我只在请求中看到多部分/表单数据,但参数字段是{}。。。。。请帮忙 Host: localhost:8888 Connection: keep-alive Content-Length: 143 Origin: http://localhost:8888 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/
Host: localhost:8888
Connection: keep-alive
Content-Length: 143
Origin: http://localhost:8888
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryUCJkE7tIQ5AqLm74
Accept: */*
Referer: http://localhost:8888/
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6
请求负载看起来像
------WebKitFormBoundary93G8teUQTA7hGmxn
Content-Disposition: form-data; name="action"
insert
------WebKitFormBoundary93G8teUQTA7hGmxn--
我使用Javaservlet作为服务器端,其中
String action = request.getParameter("action");
retrunnull
下面是我的代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Distributed Storage System</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="result"></div>
<form id="form">
<table>
<tr>
<td colspan="2" style="font-weight:bold;">Insert:</td>
</tr>
<tr>
<td>Key:<input type="text" id="insertKey" name="key"/></td>
<td>File:<input type="file" id="insertValue" name="file"/></td>
<td><input type="button" value="Insert" id="insertValue" onClick="insert()"/></td>
</tr>
</table>
</form>
<script type="text/javascript">
function insert(){
var data = new FormData();
data.append('action','insert');
//var file = $("#insertValue")[0].files[0];
var xhr = new XMLHttpRequest();
xhr.open( 'POST', '/distributedstorage', true );
xhr.send( data );
}
</script>
</body>
</html>
分布式存储系统
插入:
关键:
文件:
函数插入(){
var data=new FormData();
data.append('action','insert');
//var file=$(“#insertValue”)[0]。文件[0];
var xhr=new XMLHttpRequest();
xhr.open('POST','/distributedstorage',true);
发送(数据);
}
对于内容类型:多部分/表单数据
我们应该使用FileItem
方法getFieldName()getString()
来获取常规表单字段数据。在这里,我提供了来自文档的更多详细信息和代码片段,以便使用
ServletFileUpload
读取servlet
中的所有表单字段数据
有关此处理的理解,请参见下面的代码剪贴:
处理上载的表单元素内容(包括常规字段和文件)
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
处理文件上传
// processFormField
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
...
}
// processUploadedFile
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
...
}
请继续执行中的其余代码看起来很好--查看
标题下的请求有效负载
,很抱歉,但“查看标题下的请求有效负载”是什么意思。请求HeaderOK下的有效负载,我在chrome中检查,它显示----WebKitFormBoundary93G8teUQTA7hGmxn内容配置:表单数据;name=“action”insert----WebKitFormBoundary93G8teUQTA7hGmxn--看起来它就在那里,但是为什么我的servlet不接收它呢?String action=request.getParameter(“action”);