简体   繁体   English

为什么我的网站上突然出现“没有 'Access-Control-Allow-Origin' header is present”错误?

[英]Why am I suddenly getting a “No 'Access-Control-Allow-Origin' header is present” error on my website?

I was creating server blocks in my DigitalOcean server aso I could host two websites from the same droplet and I think I got it to work but the website was broken.我在我的 DigitalOcean 服务器中创建服务器块,以便我可以从同一个 droplet 托管两个网站,我想我让它工作了,但网站坏了。 I opened my code in VSCode and ran it with npm start and nodemon and I'm getting this error when I try to pull data from the Pokeapi:我在 VSCode 中打开我的代码并使用 npm start 和 nodemon 运行它,当我尝试从 Pokeapi 中提取数据时出现此错误:

Access to XMLHttpRequest at ' https://pokeapi.co/api/v2/pokemon/charizard/ ' from origin ' http://localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Access to XMLHttpRequest at ' https://pokeapi.co/api/v2/pokemon/charizard/ ' from origin ' http://localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header 存在于请求的资源上。

This is the same error I get on my live website.这与我在实时网站上遇到的错误相同。 I've spent the last few hours trying to figure out what I can do to no avail.我花了最后几个小时试图弄清楚我能做什么无济于事。 I'm wondering if this is a problem with the Pokeapi or with me because I went on my phone to the website and it's working perfectly fine.我想知道这是 Pokeapi 的问题还是我的问题,因为我用手机访问了该网站,它运行良好。 I'm really bummed because I need to find a job and I don't think anyone would want to hire me with a broken website.我真的很沮丧,因为我需要找到一份工作,而且我认为没有人愿意在网站损坏的情况下雇用我。 If anyone wants to see parts of my code I will show it.如果有人想查看我的部分代码,我会展示它。 Any help is much appreciated.任何帮助深表感谢。

With the help of Kasey Chang I was able to figure out a fix.在 Kasey Chang 的帮助下,我找到了解决办法。 This is how the top of my server file looked before the fix:这是我的服务器文件顶部在修复之前的样子:

require('dotenv').config();
const {CONNECTION_STRING, SESSION_SECRET, SERVER_PORT} = process.env;
const express = require('express');
const app = express();
app.use(express.json());
const massive = require('massive');
const session = require('express-session');

And I created my own proxy so now it looks like this:我创建了自己的代理,所以现在看起来像这样:

require('dotenv').config();
const {CONNECTION_STRING, SESSION_SECRET, SERVER_PORT} = process.env;
const express = require('express');
const app = express();

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    next();
});

app.use(express.json());
const massive = require('massive');
const session = require('express-session');

Now my website is working again.现在我的网站又可以工作了。 I hope this helps anyone that runs into the same problem.我希望这可以帮助遇到同样问题的任何人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到没有“访问控制允许来源”错误? - Why am I getting a No 'Access-Control-Allow-Origin' error? 尝试使用Ajax访问页面时,为什么会出现Access-Control-Allow-Origin错误? - Why am I getting Access-Control-Allow-Origin error when trying to use ajax to access a page? 不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present Zillow API错误:“不存在“ Access-Control-Allow-Origin”标头” - Zillow API Error:“No 'Access-Control-Allow-Origin' header is present” 错误在请求的资源上没有“ Access-Control-Allow-Origin”标头 - Error No 'Access-Control-Allow-Origin' header is present on the requested resource AJAX请求中没有“ Access-Control-Allow-Origin”标头存在错误 - No 'Access-Control-Allow-Origin' header is present error in AJAX Request “没有'Access-Control-Allow-Origin'标题存在”与Cherrypy错误 - “no 'Access-Control-Allow-Origin' header is present” error with Cherrypy No 'Access-Control-Allow-Origin' header is present on the requested resource error - No 'Access-Control-Allow-Origin' header is present on the requested resource error 当我使用公共URL时,请求的资源上没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource when i am using a public URL 为什么我的 JavaScript 代码收到“请求的资源上不存在‘Access-Control-Allow-Origin’header”错误,而 Postman 却没有? - Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM