简体   繁体   English

API 请求被 CORS 阻止

[英]API request blocked by CORS

I'm trying to use the g-trends API in my vuejs application running from localhost, but keep running into issues of my requests getting blocked due to CORS restrictions: No 'Access-Control-Allow-Origin' header is present on the requested resource.我正在尝试在从 localhost 运行的 vuejs 应用程序中使用 g-trends API,但由于 CORS 限制,我的请求被阻止的问题不断出现:No 'Access-Control-Allow-Origin' Z099FB995346F331C5E39资源。

I know how to fix the issue when performing a fetch request, but not in a scenario where I don't actually see the request URL like when using an API.我知道在执行获取请求时如何解决问题,但不是在我实际上没有看到请求 URL 的情况下,就像使用 API 时一样。 Is there a way to fix this?有没有办法来解决这个问题?

const { ExploreTrendRequest } = require('g-trends');
const explorer = new ExploreTrendRequest();
explorer.past5Years()
        .addKeyword("keyword")
        .download().then(csv => {
          console.log(csv)                         
        })

I usually use a proxy server for avoiding this situation, your app asks your server to do that request for you so the communication with the API is managed by your server, when your server receives the answer it forwards it to your app and there won´t be any CORS problem.我通常使用代理服务器来避免这种情况,您的应用程序要求您的服务器为您执行该请求,因此与 API 的通信由您的服务器管理,当您的服务器收到答案时,它会将其转发给您的应用程序并且不会不是任何 CORS 问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM