skip to Main Content

Cookie not show in browser : i try again and again but cookie send from backend but not store in browser, i also try another browser like chrome and microsoft bing
this is node js code and html, javascript

in postman cookies store but in broswer not store

**backend code :

**

const express = require('express')
const cookieParser = require('cookie-parser')
const cors = require('cors')
const app = express();
app.listen(8080,()=>{
    console.log("express")
})
app.use(cookieParser())
app.use(cors())

app.get('/login',(req,res)=>{
    res.cookie('cookies','myid',{
        httpOnly:true
    }).json({msg:"cookies sended ! "})
})

**front end code :

**

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button onclick="myfunction()">Fetch</button>

    <script>
        const myfunction = async ()=>{
        let res = await fetch('http://localhost:8080/login',{ method:"GET"})
        res = await res.json()
        console.log(res)
        }
    </script>
</body>
</html>

output in consol :

{ msg: 'cookies sended ! '}

i am expecting :
cookies store in browser

2

Answers


  1. Make sure that cookies are enabled in your browser. Most modern browsers allow cookies by default, but it’s possible that you or a browser extension have disabled them.

    Login or Signup to reply.
  2. If you are opening the HTML file directly in your browser without a web server, you may encounter issues with cookies because some browser security policies restrict cookies in local file contexts. To work with cookies reliably, it’s best to serve your HTML file through a local web server even during development.

    Moreover, send the request directly to the "localhost:8080/login" endpoint through the browser instead of sending request through frontend file. Now, the cookie will be stored in the browser.

    If you click the fetch button in the frontend code, you will see that cookies are being sent in the response but they are not being stored in the browser:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search