'I want to fetch user information from index page using fetch

  1. After successful login, you will be directed to /index.
  2. Retrieve user information by using fetch with JavaScript within the index page.
  3. At this time, it moves to the url used for fetch, not to the index page. Why? I just want to apply the value of the object response from fetch to index.html.

This is the code in error

//index.html
<html>
 <head></head>
 <body>
  <div id='user'></div>
  <script src="./index.js"></script>
 </body>
</html>

//index.js
const id = document.getElementById('user');
fetch('/api/user')
.then((res)=>res.json)
.then((data)=>{
 id.innerText = data.userid})

When you access the index page, the corresponding screen appears.



Solution 1:[1]

The problem is probably in your redirection/login code, not what you posted here. If you post that, maybe we could help further.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piotr Płaczek