簡體   English   中英

無法從HttpServletRequest讀取表單數據

[英]Can't read form data from HttpServletRequest

我有一個簡單的xmlHttpRequest,它向Servlet提交DELETE。

這是JS:

        function sendDELETE () {
            var http = new XMLHttpRequest();
            var url = "myServlet";
            var params = "param="+document.getElementById("param").value;
            http.open("DELETE", url, false);

            //Send the proper header information along with the request
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {
                }
            }
            http.send(params);
        }

這由按鈕上的onClick事件調用。

chrome開發工具顯示的內容如下:

Request URL:http://xxxxxx/myServlet
Request Method:DELETE
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:36
Content-type:application/x-www-form-urlencoded
Host:xxxxxxxxx
Origin:http://xxxxxxx
Referer:http://xxxxxxxxxxx/main.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
Form Dataview parsed
param=value


Response Headersview source
    Connection:Keep-Alive
    Content-Length:0
    Content-Type:text/html
    Date:Wed, 27 Aug 2014 01:50:10 GMT
    Keep-Alive:timeout=65, max=7998
    Server:Apache-Coyote/1.1

通過查看此內容,我可以清楚地看到表單數據在請求中具有param = value

doDelete服務器上,我嘗試了兩種方法:

String param = req.getParameter("param");

String inputMap = CharStreams.toString(req.getReader()); String [] parts = inputMap.split("="); String param = parts[1];

但是都不起作用-'param'仍然為null ....

我究竟做錯了什么 ?

僅POST請求在請求正文中具有作為實體的參數。 您必須將其作為URL的一部分發送。

請參閱RFC 2616 :“ DELETE方法請求源服務器刪除由Request-URI標識的資源”。

據我所知,您根本沒有做錯任何事情。

在HTTP中,任何請求都可以具有請求正文(請參見http://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7230.html#message.body.length )。 同樣,HTTP不在乎有效負載的類型

發送DELETE的有效載荷是否有意義是一個單獨的問題。 畢竟,無論如何,都需要通過請求URI來標識要刪除的資源。

如果您無法在有效負載中檢索請求正文,則可能是由於Servlet實現中的錯誤(是嗎?)或客戶端與服務器之間的某種原因(代理?servlet過濾器?)引起的。

暫無
暫無

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

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