如何访问 API 而不会遇到允许访问控制源问题?
我注意到这似乎是一个普遍问的问题,但是到目前为止,我见过的任何回答都没有帮助。 我正在尝试使用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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论