2

我正在使用 POST 方法向 REST(eXist db) Web 服务提交 HTML 表单。正常提交是给出400 错误请求

这是我的 HTML 代码

<html>
    <script type="text/javascript">
     /* function createXMLHttpRequest()
       {
        if( typeof XMLHttpRequest == "undefined" )
        XMLHttpRequest = function() 
         {
          try 
          { 
           return new ActiveXObject("Msxml2.XMLHTTP.6.0")
          } 
         catch(e) {}
          try 
          { 
           return new ActiveXObject("Msxml2.XMLHTTP.3.0")
          } 
         catch(e) {}
          try
          { 
          return new ActiveXObject("Msxml2.XMLHTTP") 
          } 
         catch(e) {}
          try 
          { 
          return new ActiveXObject("Microsoft.XMLHTTP") 
          } 
         catch(e) {}
         throw new Error( "This browser does not support XMLHttpRequest." )
      };
       return new XMLHttpRequest();
     }

var AJAX = createXMLHttpRequest();*/
function submitForm()
 {

    //AJAX.open("POST",'http://localhost:8899/exist/rest/db/xql/sample.xq');
   // AJAX.send(document.form.xmlData.value);
   document.form.submit();
 };
</script>
<head>  
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
 <form name='form' action="http://localhost:8899/exist/rest/db/xql/sample.xq"  enctype="text/plain" method="post">
   <input type="text" name="xmlData"/>
   <input type="button" value="Submit" onclick="submitForm()";>
 </form>
</body>
</html>

注释代码是使用 AJAX 发送 POST 请求。我捕获了表单提交和 AJAX 提交的 http 标头请求和响应这些是请求标头:

HTML 表单提交标头:

(Request-Line)  POST /exist/rest/db/xql/sample.xq HTTP/1.1
Host    localhost:8899
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Connection  keep-alive
Content-Type    text/plain
Content-Length  26

AJAX 请求头:

(Request-Line)  POST /exist/rest/db/xql/sample.xq HTTP/1.1
Host    localhost:8899
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Connection  keep-alive
Content-Length  16
Content-Type    text/plain; charset=UTF-8
Origin  null
Pragma  no-cache
Cache-Control   no-cache

我没有明白我的代码有什么问题。我为此工作了 2 天,但我找不到任何解决方案。请对此进行调查并提供解决方案。

提前致谢。

4

4 回答 4

0

您的代码按应有的方式将数据汇总到服务器。您的服务器端代码一定有问题。

引用 checkupdown.com 关于错误 400

HTTP 周期中的 400 错误

1.任何客户端(例如您的 Web 浏览器或我们的 CheckUpDown 机器人)都会经历以下循环:

2.从站点的IP名称(不带'http://'的站点URL)中获取IP地址。此查找(IP 名称到 IP 地址的转换)由域名服务器 (DNS) 提供。

3.打开到该 IP 地址的 IP 套接字连接。

4.通过该套接字写入一个HTTP数据流。

5. 接收来自 Web 服务器的 HTTP 数据流作为响应。此数据流包含状态代码,其值由 HTTP 协议确定。解析此数据流以获取状态代码和其他有用信息。

当客户端收到它识别为“400”的 HTTP 状态代码时,会在上述最后一步中发生此错误。

于 2012-05-30T06:07:14.760 回答
0

但是您没有使用 Ajax POST 发送任何参数?

Ajax 代码应如下所示:

var xmlData=encodeURIComponent(document.getElementById("xmlData").value);
var parameters="xmlData="+xmlData;
AJAX.open("POST", "'http://localhost:8899/exist/rest/db/xql/sample.xq", true)
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
AJAX.send(parameters)
于 2012-05-30T07:12:01.193 回答
0

我很确定这是因为您只发送数据中的值。

您需要发送一个名称 = 值对。

于 2012-05-30T05:58:59.590 回答
0

你的目标接受 POST 请求,还是只接受 GET?

于 2012-05-30T06:32:51.283 回答