简体   繁体   English

请求的资源上不存在 Access-Control-Allow-Origin header。 将请求的模式设置为 no-cors 以获取禁用 CORS 的资源

[英]No Access-Control-Allow-Origin header is present on the requested resource. set the request's mode to no-cors to fetch the resource with CORS disabled

has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.已被CORS策略阻止:请求的资源上不存在“访问控制允许来源” header。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源

The URL downloading a txt file in web browser https://google.com/complete/search?client=chrome&q=python URL在web浏览器https://google.com/complete/search?client=chrome&q=python中下载txt文件

import './App.css';
import {useState} from 'react';

function App() {
  const [Keyword, setKeyword] = useState("");
  const [Result, setResult] = useState([]);

  const findMatch = async () => {
    const getMatch = await fetch("http://google.com/complete/search?client=chrome&q=" + Keyword, {
      method: 'GET',
      headers: {
      'Access-Control-Allow-Origin': true,
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582',
      'Content-Type': 'text/plain',
      },
    })
    console.log(getMatch);
  }

  return (
    <div className="App">
      <input placeholder='Put Keyword' value={Keyword} onChange={(e) => setKeyword(e.target.value)} />
      <button onClick={findMatch}>Search</button>
    </div>
  );
}

export default App;

CORS is a server-side origin checking service where you can allow or disallow certain origins to request your resources. CORS 是一种服务器端来源检查服务,您可以在其中允许或禁止某些来源请求您的资源。 Given this, your header option 'Access-Control-Allow-Origin': true would be returned from the server in a RESPONSE header, while you're trying to put it in the REQUEST header and this will not work.鉴于此,您的 header 选项'Access-Control-Allow-Origin': true将在响应 header 中从服务器返回,而您正在尝试将其放入 REQUEST Z099FB995346F331C5E3Z 中,而这将不起作用。 You can try disabling CORS on your request, although this will allow for less headers in your request:您可以尝试在您的请求中禁用 CORS,尽管这将允许您的请求中的标头更少:

const getMatch = await fetch("http://google.com/complete/search?client=chrome&q=" + Keyword, {
      method: 'GET',
      mode: 'no-cors',
      headers: {
      'Content-Type': 'text/plain',
      },
    })

Source: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options来源: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options

You can use a cors proxy service for that.您可以为此使用 cors 代理服务。 for example, cors.sh例如, cors.sh

暂无
暂无

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

相关问题 CORS 政策阻止了获取“url”的访问:请求的资源上不存在“Access-Control-Allow-Origin”header。 ReactJS - Access to fetch `url` been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. ReactJS CORS问题-“所请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头。” - CORS-issue - “No 'Access-Control-Allow-Origin' header is present on the requested resource.” CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”标头。 在 reactjs 中使用 iframe 时 - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. while using iframe in reactjs CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略阻止了对获取的访问:请求的资源上不存在“Access-Control-Allow-Origin”header - Access to fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 获取已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 如何修复“http://localhost:3000”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”header。 - How to fix ''http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.' 被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”header。 Java 带有 CrossOrigin("*") 注释的后端 - Blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Java Backend with CrossOrigin("*") annotation 请求的资源上不存在“Access-Control-Allow-Origin”header(AWS、API 网关、S3、CORS) - No 'Access-Control-Allow-Origin' header is present on the requested resource (AWS, API Gateway, S3, CORS) Amazon S3奇怪的CORS行为:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Amazon S3 weird CORS behaviour: No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM