简体   繁体   English

无法使用 header 向 api 发出获取请求

[英]Can't make a get request with header to an api

I'm trying to make a get request to an api,but there is en error: Failed to fetch, what might be wrong here?我正在尝试向 api 发出获取请求,但出现错误:无法获取,这里可能有什么问题?

 const getData = () => { fetch("https://test-docs.stores.kg/api/categories", { method: "GET", headers: { secretKey: "test_key", accept: "application/json " } }).then((res) => res.json()).then((res) => console.log(res)); }; getData();

Seems like the API you're querying is not setting a CORS header (check for reference https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS ). Seems like the API you're querying is not setting a CORS header (check for reference https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS ). Therefore your request is being blocked.因此,您的请求被阻止。 cors_issue

If you are in control of this API adjust the webserver settings to set the appropriate header: eg如果您控制此 API,请调整网络服务器设置以设置适当的 header:例如

  • "Access-Control-Allow-Origin: https://YOUR_WEB_FRONTEND_DOMAIN.TLD" “访问控制允许来源:https://YOUR_WEB_FRONTEND_DOMAIN.TLD”
  • (or if you just want to get it to work in a development setup) "Access-Control-Allow-Origin: *" (或者如果你只是想让它在开发设置中工作)“Access-Control-Allow-Origin:*”

You can also test the API in this Tool here https://www.test-cors.org/#?client_method=GET&client_credentials=false&server_url=%20https%3A%2F%2Ftest-docs.stores.kg%2Fapi%2Fcategories&server_enable=true&server_status=200&server_credentials=false&server_tabs=remote您也可以在此工具中测试 API https://www.test-cors.org/#?client_method=GET&client_credentials=false&server_url=%20https%3A%2F%2Ftest-docs.stores&server_enableFZ=true%2server_docs.stores&server_enableFZ =200&server_credentials=false&server_tabs=远程

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

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