反应:使用AXIOS不起作用的local主机呼叫外部API
我正在使用NextJS制作一个简单的CRUD应用程序。该应用程序目前是本地的,尚未部署任何地方。我正在尝试对外部API进行API调用:
import axios from "axios";
let headers = {
"token": process.env.NEXT_PUBLIC_BARCODELOOKUP_API_KEY,
};
export const getRecordInfoByBarcode = async (barcode) => {
const searchByBarcode = `https://api.barcodelookup.com/v3/products?upc=${barcode}&formatted=y`;
if (barcode === "" || barcode === undefined) {
throw "Barcode cannot be empty!";
}
const res = await axios.get(searchByBarcode, { headers })
.then(response => {
console.log("response from barcodelookup");
console.log(response);
})
.catch(e => console.log(e))
}
但是我会收到以下错误:
从'https://api.barcodelookup.com/v3/products?upc = 724381033210& formatted = y'访问xmlhttprequest'前飞行请求不传递访问控制检查:未在请求的资源上显示“访问控制”标题。
根据API的文档( https://devapi.barcodespider.com/documentation.com/documentation )标题中的令牌
作为URL的一部分: https://api.barcodelookup.com/v3/products?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt = 0296667003315
我已经尝试过,无效。还尝试了我的后端应用程序(GO编写)的相同电话,但是我得到了相同的结果。
我可以从邮递员等REST客户端执行呼叫。
API文档没有提供注册我的应用程序的方法。
I'm working on a simple CRUD app with Nextjs. The app is currently local, not deployed anywhere yet. I'm trying to make an API call to an external API:
import axios from "axios";
let headers = {
"token": process.env.NEXT_PUBLIC_BARCODELOOKUP_API_KEY,
};
export const getRecordInfoByBarcode = async (barcode) => {
const searchByBarcode = `https://api.barcodelookup.com/v3/products?upc=${barcode}&formatted=y`;
if (barcode === "" || barcode === undefined) {
throw "Barcode cannot be empty!";
}
const res = await axios.get(searchByBarcode, { headers })
.then(response => {
console.log("response from barcodelookup");
console.log(response);
})
.catch(e => console.log(e))
}
but I'm getting the following error:
Access to XMLHttpRequest at 'https://api.barcodelookup.com/v3/products?upc=724381033210&formatted=y' from origin 'http://localhost:3001' 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.
According to the API's documentation (https://devapi.barcodespider.com/documentation) you can either send the token
in the header or as part of the URL:https://api.barcodelookup.com/v3/products?token=XXXXXXXXXXXXXXXXXX&upc=029667003315
I've tried both ways and none worked. Also tried the same call from my backend app (which is written in Go) but I'm getting the same results.
I can perform the call from a REST client such as POSTMAN.
The API docs do not offer a way to register my app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过记录
process.env.next_public_barcodelookup_api_key
到控制台?如果您已经使用Create React应用程序创建了应用程序,则需要将环境变量带有react_app的前缀,例如react_app_next_public_barcodelookulookup_api_key
Have you tried logging
process.env.NEXT_PUBLIC_BARCODELOOKUP_API_KEY
to the console? If you have created your app using Create React App then you need to prefix the environment variables with REACT_APP likeREACT_APP_NEXT_PUBLIC_BARCODELOOKUP_API_KEY