2016-06-22 2 views
0

Ich möchte eines der Felder eines HTML-Formulars verwenden und es verwenden, um die URL für die Aktion zu ändern. Ich folgte , die zeigt, wie es für eine GET-Anfrage gemacht wird, aber ich habe Probleme beim Ändern der Technik für eine POST-Anfrage.An eine HTML-Formular-URL aus einer POST-Anforderung anfügen

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>User location lookup</title> 
    </head> 

    <body> 
     <!-- method: https://stackoverflow.com/questions/11811782/form-value-creates-a-url --> 

     <script> 
      function process() 
      { 
       var url="http://localhost:43/" + document.getElementById("url").value; 
       location.href=url; 
       return false; 
      } 
     </script> 

     <form onSubmit="return process();" method="get"> 
      Username: <input type="text" name="url" id="url"></input> 
      <input type="submit" value="go"></input> 
     </form> 

     <script> 
      function process2() 
      { 
       var url="http://localhost:43/" + document.getElementById("url").value; 
       location.href=url; 
       return false; 
      } 
     </script> 

     <form method="post" onSubmit="return process2();"> 
      Username: <input type="text" name="url" id="url"> </input> 
      Location: <input type="text" name="location" id="location"> </input> 
      <input type="submit" value="go"></input> 
     </form> 
    </body> 
</html> 

Die GET-Anfrage korrekt sendet:

GET /brian HTTP/1.1 
Accept: text/html, application/xhtml+xml, */* 
Accept-Language: en-GB 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko 
Accept-Encoding: gzip, deflate 
Host: localhost:43 
DNT: 1 
Connection: Keep-Alive 

Während für die POST ich:

GET /brian HTTP/1.1 
Accept: text/html, application/xhtml+xml, */* 
Accept-Language: en-GB 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko 
Accept-Encoding: gzip, deflate 
Host: localhost:43 
DNT: 1 
Connection: Keep-Alive 

, die noch eine GET ist, auch wenn das Verfahren POST ist.

Was würde Ich mag erzeugen ist:

POST /brian HTTP/1.1 
Accept: text/html, application/xhtml+xml, */* 
Accept-Language: en-GB 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko 
Accept-Encoding: gzip, deflate 
Host: localhost:43 
Content-Length: 11 
DNT: 1 
Connection: Keep-Alive 

location=SO 

Kann mir jemand in die richtige Richtung weisen, mit so wenig JavaScript-Code wie möglich?

Antwort

3

Ihr Code übermittelt das Formular nicht. Es ruft tatsächlich direkt die URL über GET, das heißt: Diese Methode funktioniert nur mit GET.

Bitte versuchen:

<form id="myformid" method="post"> 
    Username: <input type="text" name="url" id="url2" onChange="updateAction()"> </input> 
    Location: <input type="text" name="location" id="location"> </input> 
    <input id="mysubmit" type="submit" value="go"></input> 
</form> 

<script> 
    function updateAction() { 
     var url = document.getElementById("url2").value; 
     document.getElementById("myformid").setAttribute("action", "http://localhost:43/" + url); 
    } 
</script>