Avatar ·

How to send a POST request in JavaScript simulating HTML form submission?

Hi everyone! I'm new to JavaScript and encountered this problem. I need to send a POST request to the server with data, but the server expects the data to come in `application/x-www-form-urlencoded` format, as if it was sent by a regular HTML form. I tried using `fetch` with JSON, but the server doesn't accept such data — it returns a 400 error. Here's my code that doesn't work: ```javascript fetch('/api/submit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'John', age: 30 }) }); ``` The server says it doesn't see the `name` and `age` fields. I read that you can somehow emulate form submission, but I don't understand how to do it correctly. I need the data to be sent in the same format as when submitting a `
` with