要让GraphQL在CORS中运行,可以采取以下步骤:
app.use(cors());
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("AllowAllOrigins");
}
fetch('https://api.example.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Origin': 'https://www.example.com' // 设置请求的来源域
},
body: JSON.stringify({ query: '...' })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
const { ApolloServer } = require('apollo-server-express');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
const server = new ApolloServer({ / 配置项 / });
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
领取专属 10元无门槛券
手把手带您无忧上云