如何访问 API 而不会遇到允许访问控制源问题?

发布于 2025-01-19 00:13:38 字数 1087 浏览 0 评论 0原文

我注意到这似乎是一个普遍问的问题,但是到目前为止,我见过的任何回答都没有帮助。 我正在尝试使用Axios调用访问我的JavaScript+TypeScript应用程序中的API(杜鹃沙盒,在VM上设置)。这就是称为API的代码。

      axios({
    method: 'get',
    url: 'http://192.168.1.220:1337/tasks/view',
    headers: {          
     'Authorization': 'Bearer WoRKpl5',
    //  'Allow-Control-Allow-Origin': '*'
  },
  }).then(resp =>{
    console.log("Hi")
  }).catch(err => { // then print response status
    console.log(err)
  });

响应如下所示:

到目前为止,我尝试编辑杜鹃的api_py文件,以在do_post函数中包含标头,修改Axios调用中的标题(评论),在禁用Web安全后运行Chrome浏览器,试图尝试尝试禁用CORS(不确定成功),并使用在Heroku上托管的代理(CORS-NONDHERE)添加标题,除其他方面,但没有太大的成功。杜鹃花盒不断收到选项请求。这是杜鹃文件中当前标头的屏幕截图:

“标头图像”

我发现有趣的是,通过常规命令行界面和curl命令,API正在工作,我们可以成功访问它。 关于如何成功地从申请中获得get和发布请求的应用程序,任何进一步的见解都是有帮助的。

I've noticed that this seems to be a commonly asked question, but none of the responses I've seen have helped so far.
I'm trying to access an API (Cuckoo Sandbox, set up on a VM) from my JavaScript+TypeScript application using an Axios call. This is what the code to call the API looks like.

      axios({
    method: 'get',
    url: 'http://192.168.1.220:1337/tasks/view',
    headers: {          
     'Authorization': 'Bearer WoRKpl5',
    //  'Allow-Control-Allow-Origin': '*'
  },
  }).then(resp =>{
    console.log("Hi")
  }).catch(err => { // then print response status
    console.log(err)
  });

The response is as shown below:
Image of response

So far, I've tried editing the api_py file for cuckoo to include headers in the do_post function, modifying the headers in the axios call (commented out), running Chrome browser after disabling web security, trying to disable CORS (unsure if successful) and using a Proxy (CORS-anywhere) hosted on Heroku to add headers, among other things, without much success. Cuckoo Sandbox constantly receives only Options requests. Here is a screenshot of the current headers in the Cuckoo file:

Image of headers

I found it interesting to note that through the regular command line interface and the curl command, the API was working and we could successfully access it.
Any further insight on how to successfully hit the API from the application for both GET and POST requests would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文