0

When I click on submit button, the code works, the tittle changes to the random band name, but when i click a second time, it just do nothing. I don't understand what the problem could be, but when I click submit, and "/submit" to get accessed it shows "?" after in the each bar .I am using express and EJS.

app.get("/", (req, res)=>
{
    res.render("index.ejs");
});
app.get("/submit", (req, res)=>
{
    res.render("index.ejs", { randomName: sillyName});
});
<body>
    <% if (locals.randomName) { %>
        <h1><%= randomName %> Band</h1>
        <% } else { %>
          <h1>Band name Generator </h1>
          <% } %>
        <form action="/submit" method="GET">
                <input type="submit" value="Start" id="subbutone">
        </form>
</body>
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    You are using GET to return data that is not [idempotent](https://stackoverflow.com/questions/45016234/what-is-idempotency-in-http-methods). You should be using POST if the response from the server is different every time. Otherwise the browser is likely free to cache the result of the page fetched with GET and display it without actually fetching data from the server again. This, of course, depends on [what you have done with respect to caching and headers](https://stackoverflow.com/questions/22632593/how-to-disable-webpage-caching-in-expressjs-nodejs) in your express app. – Wyck Aug 02 '23 at 19:49
  • it shows "Cannot GET /submit". When i click first time, it work, it generate a random name, but secnd time it do nothing – King Federico Aug 02 '23 at 19:56
  • 1
    Although I got it to work with GET, what Wyck said is exactly right - POST makes most sense in this case. Here you go anyway: https://stackblitz.com/edit/expressjs-yatr9r?file=index.js – Christian Aug 02 '23 at 19:56
  • 1
    You need to change both `method="GET"` as well as `app.get('/submit` - and also make sure your browser isn't still on the `/submit` url. ;) – Christian Aug 02 '23 at 19:58
  • thank you all!! – King Federico Aug 02 '23 at 20:05

0 Answers0