簡體   English   中英

為什么我的formdata.append沒有將鍵值對發送到服務器?

[英]Why my formdata.append is not sending key value pair to server?

我認為這應該是非常簡單的jquery代碼,但是我看不到它將任何結果傳遞給服務器。 我現在只想測試字符串,不需要文件! 在服務器中,我僅在請求中看到multipart / form-data,但是參數字段為{} .....請幫助...

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--

我使用Java Servlet作為服務器端,

String action = request.getParameter("action");

重新運行null

下面是我的代碼

<%@ 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>

對於Content-Type: multipart/form-data我們應該使用FileItem方法getFieldName() getString()來獲取常規表單字段數據。
在這里,我將提供帶有片段的文檔中的更多詳細信息,以使用ServletFileUpload讀取Servelet所有表單字段數據

查看用於處理上傳項目的apache-docs

請參閱以下代碼片段以了解此處理過程:

處理上載的表單元素同時包含常規字段和文件

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

請繼續http://commons.apache.org/proper/commons-fileupload/using.html中的其余代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM