Javascript 不能';t使用fetch函数设置cookie

Javascript 不能';t使用fetch函数设置cookie,javascript,node.js,reactjs,react-native,express,Javascript,Node.js,Reactjs,React Native,Express,我的express服务器正在端口3001上运行 在REACT文件中 在我的登录页面上 fetch('http://localhost:3001/api/login',{ method:'POST', headers: { Accept: 'application/json', "Content-Type": "application/json", }, body:JSON.string

我的express服务器正在端口3001上运行

在REACT文件中

在我的登录页面上

fetch('http://localhost:3001/api/login',{
        method:'POST',
        headers: {
            Accept: 'application/json',
            "Content-Type": "application/json",
        },
        body:JSON.stringify(this.state),
        credentials: 'same-origin',
        mode:'cors'
    }).then((respo)=>{
        console.log(respo);
    })
在主页上

fetch('http://localhost:3001/api/home',{credentials:'same-origin'}).then((response)=>{
        return response.json();
    }).then((response)=>{
        this.setState({
            isLoading:false,
            userData:response.user,
            posts:response.posts
        })
    })
当我在登录页面上设置cookie时,从开发工具中我可以看到我的cookie正在被设置。但是我没有收到主页上的设置Cookie。我哪里出了问题

编辑:设置cookie(服务器端)


您的API服务器是
localhost:3001

您的页面从
127.0.0.1:3000加载

这些是不同的起源

您说了
凭证:“同一来源”
,因此跨来源凭证(包括cookie)被禁用

您需要
include
,才能为跨来源呼叫发送cookies


另请参见:。

设置cookie的代码在哪里?我已尝试在localhost:3001上托管我的服务器,但结果保持不变。当我将凭据更改为包含时,会出现cors问题。错误:
Access to fetch at'http://localhost:3001/api/login“起源”http://127.0.0.1:3000'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:中的'access control Allow Credentials'标头的值响应为“”,当请求的凭据模式为“包括”时,该响应必须为“真”。
@AniketTyagi因此请更改传递给
cors()
的配置以启用该响应。该模块的文档非常好。
    const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const sessions = require('client-sessions');
const Post = require('./model/post');
const User = require('./model/user');

mongoose.connect('mongodb://localhost/social_network',{
    useNewUrlParser:true,
    useCreateIndex:true
});

const app = express();
app.use(cors({
    origin:"http://127.0.0.1:3000"
}));
app.use(bodyParser.json())
app.use(
    bodyParser.urlencoded({
      extended: true
    })
  );

app.use(sessions({
    cookieName:'users',
    secret:'ouououou',
    duration: 24*60*60*60
}))

const auth= async(req,res,next)=>{
    try{
        console.log(req.users.user);
        const user = await User.findOne({email:req.users.email,password:req.users.password});
        if(!user){
            throw new Error("No User Found");
        }else{
            next();
        }
    }catch(e){
        console.log(e);
        res.status(404).send({error:e});
    }

}

app.post("/api/login",async (req,res)=>{
    try{
        console.log(req.body.email);
        const user = await 
User.findOne({email:req.body.email,password:req.body.password});
        if(!user){
            throw new Error("User Not Found");
        }else{
            req.users.user = user;
            console.log(req.users.user);
            res.status(200).send(req.user);
        }
    }catch(e){
        console.log(e);
        res.status(404).send({error:e})
    }
})


app.get('/api/home',auth,async (req,res)=>{
    console.log("yo");
    await req.user.populate('post').execPopulate();
    const data={user:user,post:user.post}
    res.send(data)
    });


app.listen("3001",()=>{
    console.log("on");
});