javascript - HTML 表单提交给出 400 错误请求

标签 javascript html rest post exist-db

我正在使用 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 header 请求和响应 这些是请求 header :

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 天,但我没有找到任何解决方案。 请对此进行调查并提供解决方案。

提前致谢。

最佳答案

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

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

关于javascript - HTML 表单提交给出 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10810492/

相关文章:

javascript - 在div元素的h2标签内写入本地存储值

javascript - 呈现 View 后使用 aurelia 将数据动态添加到页面

apache-flex - 对 REST 方法的灵活支持

javascript - 使用 BootStrap 和 Angular - 动态选择输入

javascript - Ember Data 没有看到我的本地商店

javascript - 从字符串创建 react 组件

c# - C# 中的 REST Web 服务

javascript - jQuery 是如何工作的 : I get "Cannot set property xxx of undefined" when trying a similar prototype thing?

html - Opera Top 位置渲染问题

java - 尝试使用 jersey 和 jaxb 时出现异常