HTML form submit giving 400 bad request

2019-07-17 21:28发布

问题:

I'm submitting a HTML form to REST(eXist db) web service using POST method.A normal submission is giving 400 bad request

Here is my HTML code

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

The commented code is to send POST request using AJAX. I captured the http header request and response for form submit and AJAX submit These are the request headers:

HTML form submit header:

(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 header:

(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

Im not getting what's wrong in my code . Im working on this for 2 days but i din't find any solution. Please look into this and provide a solution.

Thanks in advance.

回答1:

I'm pretty sure it's because you're sending only the value in your data.

You need to send a name = value pair.



回答2:

Your code sumbits data to the server as it should be. There must be some problem with your server side code.

Quoting from checkupdown.com about error 400

400 errors in the HTTP cycle

1.Any client (e.g. your Web browser or our CheckUpDown robot) goes through the following cycle:

2.Obtain an IP address from the IP name of the site (the site URL without the leading 'http://'). This lookup (conversion of IP name to IP address) is provided by domain name servers (DNSs).

3.Open an IP socket connection to that IP address.

4.Write an HTTP data stream through that socket.

5.Receive an HTTP data stream back from the Web server in response. This data stream contains status codes whose values are determined by the HTTP protocol. Parse this data stream for status codes and other useful information.

This error occurs in the final step above when the client receives an HTTP status code it recognises as '400'.



回答3:

Does your target accept POST requests, or only GET?



回答4:

But you aren't sending any parameters with the Ajax POST?

The Ajax code should look something like this:

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)