反应:使用AXIOS不起作用的local主机呼叫外部API

发布于 2025-02-09 23:28:04 字数 1370 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2025-02-16 23:28:04

您是否尝试过记录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 like REACT_APP_NEXT_PUBLIC_BARCODELOOKUP_API_KEY

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文