简体   繁体   中英

How can I keep a clean url when using a form when using javascript and react?

I'm making this form that, for now, redirects, a user to another page after submitting a form. However, after the user is redirected, the form fields pop up in the url. I'm a beginner and I was wondering on how I can keep the url path to just what I specified in the "action" attribute of the form. Here's my code

<div>
     <form action="/test">
          <input type="text" name="login-username" id="login-username" placeholder="Username" />
          <input type="password" name="login-password" id="login-password" placeholder="Password" />
          <input type="submit" name="login-submit-button" id="login-submit-button" />
     </form>
</div>

After clicking the button, the url looks like this:

http://localhost:3000/test?login-username=&login-password=&login-submit-button=Submit

How can I keep it to just

http://localhost:3000/test

Thank you.. any help would be appreciated.

As mentioned clearly in this answer , default method of form on submit is GET with encoding of type x-www-form-urlencoded , which basically appends input data to the current URL.

Using method POST appends form-data inside the body of the HTTP request (data is not shown in URL).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM