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