简体   繁体   English

在JavaScript中使用提取请求API有CORS政策错误

[英]Requesting API with fetch in javascript has CORS policy error

I'm trying to call some APIs with fetch in my javascript code. 我正在尝试在我的JavaScript代码中调用某些具有访存功能的API。 I'm developing with ReactJs in my machine and have another development in the same network developing the API with .net in another machine. 我正在使用我的机器上的ReactJs进行开发,并且在同一网络中进行另一项开发,在另一台机器上使用.net开发API。 With postman, I can call the API, but with fetch no. 使用邮递员,我可以调用API,但不能获取。 I try to call another API in other server and the result was successful. 我尝试在其他服务器上调用另一个API,结果成功。

I'm using fetch and tried to use axios too. 我使用的是获取并试图用爱可信也。 I have found in other question in stack overflow this API: https://gturnquist-quoters.cfapps.io/api/random . 我在堆栈中的其他问题中发现该API: https : //gturnquist-quoters.cfapps.io/api/random The answer says to try to fetch they and I try but throw same error again. 答案是说要尝试获取它们,而我会尝试再次抛出相同的错误。

My code to fetch the gturnquist API: 我的代码来获取gturnquist API:

const myHeader = new Headers();
myHeader.append('Content-Type', 'application/json');
fetch('http://gturnquist-quoters.cfapps.io/api/random', {
  method: 'GET', headers: myHeader,
})
 .then((res) => {
   console.log(res);
   return {};
 })
 .then(res => console.log(res));

and my code to fetch the API that i need: 和我的代码来获取我需要的API:

const myHeader = new Headers();

const token = 'mytoken';
myHeader.append('id-tenant', token);
myHeader.append('Content-Type', 'application/json');

const id = 'myid';
const url = 'myurl';

fetch(`http://10.1.1.35/${url}/${id}`, {
  method: 'GET',
  headers: myHeader,
}).then(res => res.json()).then(res => console.log(res));

I have this errors when I try to call an API 尝试调用API时出现此错误

OPTIONS http://10.1.1.35/url/id 503 (Service Unavailable) 选项http://10.1.1.35/url/id 503(服务不可用)

Access to fetch at ' http://10.1.1.35/url/id ' from origin ' http://localhost:3000 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 从起源' http:// localhost:3000 '的' http://10.1.1.35/url/id '处获取的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否'访问-Control-Allow-Origin'标头出现在请求的资源上。 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的情况下获取资源。

Uncaught (in promise) TypeError: Failed to fetch 未捕获(按承诺)TypeError:无法获取

It's a server side error or javascript error? 是服务器端错误还是javascript错误?

In my server, the API is configured like the answer of Rajkumar Peter 在我的服务器中,API的配置类似于Rajkumar Peter的回答

EDIT : My network error 编辑我的网络错误

Questions i have see to try handle my error: 我看到的问题可以尝试解决我的错误:
Fetch CORS error with instagram api 使用Instagram API获取CORS错误
Enable CORS in fetch api 在提取API中启用CORS
managing CORS with fetch API GET request 使用获取API GET请求管理CORS
React fetch has error fetch was blocked by CORS policy 反应过来取入抓取在阻止错误的CORS政策
Response to preflight request doesn't pass access control check 对预检请求的响应未通过访问控制检查

When performing an API call from a web browser, it will often do something called a "preflight check". 从网络浏览器执行API调用时,通常会执行称为“预检检查”的操作。 This is essentially where the browser sends a request to your API (ahead of your own API call), to check what it is allowed to do and whether it's even worth sending the actual request. 这主要是在浏览器发送到您的API(提前自己的API调用的)的请求,检查什么是允许做的,它是否值得,即使发送实际的请求。

To do this, it uses the OPTIONS method (instead of GET, POST .etc). 为此,它使用OPTIONS方法(而不是GET,POST等)。

If your API endpoint hasn't been configured to accept OPTIONS requests, then the preflight request from the browser will fail - and it will abort sending your request. 如果您的API端点尚未配置为接受OPTIONS请求,那么来自浏览器的预检请求将失败 - 它会中断发送您的请求。

To fix your issue, you need to configure your API to accept OPTIONS methods. 要解决您的问题,您需要将API配置为接受OPTIONS方法。 You should also check which origins (IP addresses of clients connecting to the API) are allowed. 您还应该检查允许哪些来源(连接到API的客户端的IP地址)。

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

相关问题 “访问获取已被 CORS 政策阻止” Chrome 扩展程序错误 - 'Access to fetch has been blocked by CORS policy' Chrome extension error javascript signalR 客户端:尝试连接到 api 时出现 CORS 策略错误 - javascript signalR client : CORS policy error trying to connect to api 点网核心 Web API 与 Javascript - Z5A8FEFF0B4BDE3EEC924Z 策略错误 - Dot Net Core Web API with Javascript - CORS policy error javascript:由于通配符而被CORS政策阻止 - javascript: fetch blocked by CORS policy because of wildcard 对 fetch 的访问已被 CORS 策略阻止 - Fetch() JS 问题 - Access to fetch has been blocked by CORS policy - Fetch() JS issue 使用前端 Javascript 获取 API 返回 CORS 和 302 错误 - Fetch API with frontend Javascript returns CORS and 302 error Javascript 通过正文获取请求 API 令牌 - Javascript Fetch Requesting API Token through Body Javascript 和 .NET Minimal API 中的 CORS 策略 - CORS policy in Javascript and .NET Minimal API 使用 instagram api 获取 CORS 错误 - Fetch CORS error with instagram api CORS 策略已阻止从源“http://localhost:3000”访问“https://localhost:7144/api/employees” - Access to fetch at 'https://localhost:7144/api/employees' from origin 'http://localhost:3000' has been blocked by CORS policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM